如何使用jQuery替换div的innerHTML?

本文翻译自:How to replace innerHTML of a div using jQuery?

How could I achieve the following: 我怎样才能实现以下目标:

document.all.regTitle.innerHTML = 'Hello World';

Using jQuery where regTitle is my div ID? 使用jQuery,其中regTitle是我的div ID?


#1楼

参考:https://stackoom.com/question/5UeC/如何使用jQuery替换div的innerHTML


#2楼

$("#regTitle").html("Hello World");

#3楼

The html() function can take strings of HTML, and will effectively modify the .innerHTML property. html()函数可以接受HTML字符串,并将有效地修改.innerHTML属性。

$('#regTitle').html('Hello World');

However, the text() function will change the (text) value of the specified element, but keep the html structure. 但是, text()函数将更改指定元素的(text)值,但保留html结构。

$('#regTitle').text('Hello world'); 

#4楼

Here is your answer: 这是你的答案:

//This is the setter of the innerHTML property in jQuery
$('#regTitle').html('Hello World');

//This is the getter of the innerHTML property in jQuery
var helloWorld = $('#regTitle').html();

#5楼

jQuery's .html() can be used for setting and getting the contents of matched non empty elements ( innerHTML ). jQuery的.html()可用于设置和获取匹配的非空元素( innerHTML )的内容。

var contents = $(element).html();
$(element).html("insert content into element");

#6楼

Answer: 回答:

$("#regTitle").html('Hello World');

Explanation: 说明:

$ is equivalent to jQuery . $等同于jQuery Both represent the same object in the jQuery library. 两者都代表jQuery库中的相同对象。 The "#regTitle" inside the parenthesis is called the selector which is used by the jQuery library to identify which element(s) of the html DOM (Document Object Model) you want to apply code to. 括号内的"#regTitle"称为选择器 ,jQuery库使用该选择器来标识要应用代码的html DOM(文档对象模型)的哪个元素。 The # before regTitle is telling jQuery that regTitle is the id of an element inside the DOM. regTitle之前的#告诉jQuery regTitle是DOM中元素的id。

From there, the dot notation is used to call the html function which replaces the inner html with whatever parameter you place in-between the parenthesis, which in this case is 'Hello World' . 从那里,点符号用于调用html函数,该函数用括号之间的任何参数替换内部html,在本例中为'Hello World'

发布了0 篇原创文章 · 获赞 7 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105322279