如何在jQuery中使用div的id替换div中的<ul>
,
<div id="example">
<ul>
Content To Be Replaced...........
<ul>
</div>
答案 0 :(得分:5)
$("#example ul").text('Content to replace');
您可以使用ID选择器#
选择div。空格用作后代选择器,.text
更改文本内容。您还可以使用.html
替换HTML内容。
答案 1 :(得分:4)
简单地说:
$("#example ul").html('New Content');
答案 2 :(得分:1)
我认为你的意思是这样的?
$('#example ul').html('new content');
答案 3 :(得分:-1)
绝对不需要jQuery:
var div = document.getElementById("example");
div.firstChild.nextSibling.textContent="bla"; //div
// textnode -> ul -> text
这可以压缩为:
document.getElementById("example").firstChild.nextSibling.textContent="bla";
// div --> textnode --> ul --> text
是的,它比jQuery更多的代码,但它也是a lot faster.。
你甚至可以使用:
document.querySelector('#example ul').textContent="bla";
仍然是jQuery的4-5倍,但使用非常相似的语义语法。无需删除本机JS,因为它“很难”或任何东西。