jQuery添加文本以跨越div

时间:2021-05-18 20:27:22
<div id="tagscloud">
       <span></span>
</div>

How would I do to add some text within the span like code below ?

如何在跨度中添加一些文本,如下面的代码?

<span>**tag cloud**</span>

** tag cloud **

Edit: actually the span has an id

编辑:实际上跨度有一个id

<div id="tagscloud"> <span id="WebPartCaptionWPQ2"></span> </div>

3 个解决方案

#1


100  

You can use:

您可以使用:

$("#tagscloud span").text("Your text here");

The same code will also work for the second case. You could also use:

相同的代码也适用于第二种情况。你也可以使用:

$("#tagscloud #WebPartCaptionWPQ2").text("Your text here");

#2


17  

You can use append or prepend

您可以使用追加或前置

The .append() method inserts the specified content as the last child of each element in the jQuery collection (To insert it as the first child, use .prepend()).

.append()方法将指定的内容作为jQuery集合中每个元素的最后一个子节点插入(要将其作为第一个子节点插入,请使用.prepend())。

$("#tagscloud span").append(second);
$("#tagscloud span").append(third);
$("#tagscloud span").prepend(first);

#3


17  

Careful - append() will append HTML, and you may run into cross-site-scripting problems if you use it all the time and a user makes you append('<script>alert("Hello")</script>').

小心 - append()将附加HTML,如果你一直使用它并且用户让你追加('

Use text() to replace element content with text, or append(document.createTextNode(x)) to append a text node.

使用text()将元素内容替换为文本,或者追加(document.createTextNode(x))以附加文本节点。

#1


100  

You can use:

您可以使用:

$("#tagscloud span").text("Your text here");

The same code will also work for the second case. You could also use:

相同的代码也适用于第二种情况。你也可以使用:

$("#tagscloud #WebPartCaptionWPQ2").text("Your text here");

#2


17  

You can use append or prepend

您可以使用追加或前置

The .append() method inserts the specified content as the last child of each element in the jQuery collection (To insert it as the first child, use .prepend()).

.append()方法将指定的内容作为jQuery集合中每个元素的最后一个子节点插入(要将其作为第一个子节点插入,请使用.prepend())。

$("#tagscloud span").append(second);
$("#tagscloud span").append(third);
$("#tagscloud span").prepend(first);

#3


17  

Careful - append() will append HTML, and you may run into cross-site-scripting problems if you use it all the time and a user makes you append('<script>alert("Hello")</script>').

小心 - append()将附加HTML,如果你一直使用它并且用户让你追加('

Use text() to replace element content with text, or append(document.createTextNode(x)) to append a text node.

使用text()将元素内容替换为文本,或者追加(document.createTextNode(x))以附加文本节点。