Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

javascript - 如何在没有document.write的HTML页面中显示JavaScript变量(How to display JavaScript variables in a HTML page without document.write)

I am trying to display some JavaScript variable on my HTML page.

(我试图在我的HTML页面上显示一些JavaScript变量。)

I was first using document.write() but it use to overwrite the current page when the function was called.

(我第一次使用document.write()但它用于在调用函数时覆盖当前页面。)

After searching around, the general consensus was that document.write() isn't liked very much.

(搜索后,普遍的共识是document.write()不太受欢迎。)

What are the other options?

(还有什么其他选择?)

I found a page suggesting using .innerHTML but that was written in 2005.

(我找到了一个建议使用.innerHTML的页面,但这是在2005年写的。)

A jsFiddle illustrating my problem http://jsfiddle.net/xHk5g/

(一个jsFiddle说明我的问题http://jsfiddle.net/xHk5g/)

  ask by kishan patel translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Element.innerHTML is pretty much the way to go.

(Element.innerHTML几乎是要走的路。)

Here are a few ways to use it:

(以下是一些使用它的方法:)

HTML(HTML)

<div class="results"></div>

JavaScript(JavaScript的)

// 'Modern' browsers (IE8+, use CSS-style selectors)
document.querySelector('.results').innerHTML = 'Hello World!';

// Using the jQuery library
$('.results').html('Hello World!');

If you just want to update a portion of a <div> I usually just add an empty element with a class like value or one I want to replace the contents of to the main <div> .

(如果你只是想更新<div>的一部分,我通常只需要添加一个类似value的空元素,或者我想将主内容替换为主<div> 。)

eg

(例如)

<div class="content">Hello <span class='value'></span></div>

Then I'd use some code like this:

(然后我会使用这样的代码:)

// 'Modern' browsers (IE8+, use CSS-style selectors)
document.querySelector('.content .value').innerHTML = 'World!';

// Using the jQuery library
$(".content .value").html("World!");

Then the HTML/DOM would now contain:

(那么HTML / DOM现在将包含:)

<div class="content">Hello <span class='value'>World!</span></div>

Full example.(完整的例子。) Click run snippet to try it out.(点击运行代码段进行试用。)

 // Plain Javascript Example var $jsName = document.querySelector('.name'); var $jsValue = document.querySelector('.jsValue'); $jsName.addEventListener('input', function(event){ $jsValue.innerHTML = $jsName.value; }, false); // JQuery example var $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: <input class="name" type="text" value="World"/></label> <!-- Plain Javascript Example --> <h1>Plain Javascript Example</h1>Hello <span class="jsValue">World</span> <!-- jQuery Example --> <h1>jQuery Example</h1>Hello <span class="jqValue">World</span> </body> </html> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...