如果您想了解如何在没有document.write的情况下在HTML页面中显示JavaScript变量和html没有doctype的知识,那么本篇文章将是您的不二之选。我们将深入剖析如何在没有docu
如果您想了解如何在没有document.write的情况下在HTML页面中显示JavaScript变量和html没有doctype的知识,那么本篇文章将是您的不二之选。我们将深入剖析如何在没有document.write的情况下在HTML页面中显示JavaScript变量的各个方面,并为您解答html没有doctype的疑在这篇文章中,我们将为您介绍如何在没有document.write的情况下在HTML页面中显示JavaScript变量的相关知识,同时也会详细的解释html没有doctype的运用方法,并给出实际的案例分析,希望能帮助到您!
本文目录一览:- 如何在没有document.write的情况下在HTML页面中显示JavaScript变量(html没有doctype)
- document.open() 与 document.write()的区别_javascript技巧
- document.write的几点使用心得_javascript技巧
- HTML – Netflix如何在没有页面刷新的情况下提交评级,而且没有JavaScript?
- javascript - 如何在html页面中修改所有外部链接形式?
如何在没有document.write的情况下在HTML页面中显示JavaScript变量(html没有doctype)
我正在尝试在HTML页面上显示一些JavaScript变量。我第一次使用document.write()
它,但是当调用该函数时,它用于覆盖当前页面。
在四处搜寻之后,普遍的共识是人们document.write()
对此不太喜欢。还有哪些其他选择?
我找到了一个建议使用的页面,.innerHTML
但该页面写于2005年。
答案1
小编典典Element.innerHTML
几乎是要走的路。以下是使用它的几种方法:
HTML
<div></div>
JavaScript
// ''Modern'' browsers (IE8+, use CSS-style selectors)document.querySelector(''.results'').innerHTML = ''Hello World!'';// Using the jQuery library$(''.results'').html(''Hello World!'');
如果您只想更新<div>
I 的一部分,通常只需要添加一个类似value
或类的空元素,就可以将main的内容替换为<div>
。例如
<div>Hello <spanvalue''></span></div>
然后,我将使用以下代码:
// ''Modern'' browsers (IE8+, use CSS-style selectors)document.querySelector(''.content .value'').innerHTML = ''World!'';// Using the jQuery library$(".content .value").html("World!");
然后,HTML / DOM现在将包含:
<div>Hello <spanvalue''>World!</span></div>
完整的例子。点击“运行摘要”进行尝试。
// Plain Javascript Examplevar $jsName = document.querySelector(''.name'');var $jsValue = document.querySelector(''.jsValue'');$jsName.addEventListener(''input'', function(event){ $jsValue.innerHTML = $jsName.value;}, false);// JQuery examplevar $jqName = $(''.name'');var $jqValue = $(''.jqValue'');$jqName.on(''input'', function(event){ $jqValue.html($jqName.val());});html { font-family: sans-serif; font-size: 16px;}h1 { margin: 1em 0 0.25em 0;}input[type=text] { padding: 0.5em;}.jsValue, .jqValue { color: red;}<!DOCTYPE html><html><head><script src="https://code.jquery.com/jquery-1.11.3.js"></script> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Setting HTML content example</title></head><body> <!-- This <input> field is where I''m getting the name from --> <label>Enter your name: <inputtype="text" value="World"/></label> <!-- Plain Javascript Example --> <h1>Plain Javascript Example</h1>Hello <span>World</span> <!-- jQuery Example --> <h1>jQuery Example</h1>Hello <span>World</span></body></html>
document.open() 与 document.write()的区别_javascript技巧
document.open() 打开一个新的空白文档,在IE下,open有两个默认参数,相当于document.open("text/html",''""),第二个参数只有一个值可选:replace,如果启用了该值,则新建的文档会覆盖当前页面的文档(相当于清空了原文档里的所有元素,且不能后退即,浏览器的后退按钮不可用);
看一个例子:
<script> <BR><!-- <BR>function test(){ <BR> document.open("text/html","replace"); <BR> document.writeln(Math.random()); <BR> document.write("<input type=''button'' value=''back(第二个按钮)'' onclick=''history.back()''>") <BR> document.close(); <BR> document.open("text/html",""); <BR> document.writeln(Math.random()); <BR> document.write("<input type=''button'' value=''back(第三个按钮)'' onclick=''history.back()''>") <BR> document.close(); <BR> document.open("text/html",""); <BR> document.writeln(Math.random()); <BR> document.write("<input type=''button'' value=''back(第四个按钮)'' onclick=''history.back()''>") <BR> document.close(); <BR>} <BR>//--> <BR></script>
平常都不写document.open() 与 document.close(),因为浏览器会在write之前先open一个文档,再把write的内容输出到原文档里面。write结束后,默认是不会有close的,否则第二行document.write的时候就会覆盖之前的write。
document.write的几点使用心得_javascript技巧
一直用document.write()方法向浏览器中显示数据用,把它当做Alert()使用, 看来这样用有些大材小用了,下面说说它的主要用处。
document.write()方法可以用在两个方面:
1.页面载入过程中,用脚本加入新的页面内容。
2.用延时脚本创建本窗口或新窗口的内容。
该方法需要一个字符串参数,它是写到窗口或框架中的HTML内容。这些字符串参数可以是变量或值为字符串的表达式,写入的内容常常包括HTML标记语言。如下面代码,教务系统框架载入子页
//加载效果图标

//输出框架
Index.OutputIframe = function () {
var scrolling = $.isIE6 == true ? ''yes'' : ''auto'';
document.write('''');
};