我想替换HTML锚文本:
<a href="index.html" id="link1">Click to go home</a>
现在我要替换文本“点击回家”
我已经试过了:
alert($("link1").children(":first").val());
alert($("link1").children(":first").text());
alert($("link1").children(":first").html());
但这一切都给了我null或空字符串
Answers:
尝试
$("#link1").text()
访问元素内的文本。#表示您正在按ID搜索。您不需要的是子元素,因此不需要children()。相反,您想访问jQuery函数返回的元素内的文本。
如果有ID,请尝试此操作
$("#YourId").text('Your text');
否则,如果是上课
$(".YourClassName").text('Your text');
function liReplace(replacement) {
$(".dropit-submenu li").each(function() {
var t = $(this);
t.html(t.html().replace(replacement, "*" + replacement + "*"));
t.children(":first").html(t.children(":first").html().replace(replacement, "*" +` `replacement + "*"));
t.children(":first").html(t.children(":first").html().replace(replacement + " ", ""));
alert(t.children(":first").text());
});
}
t.html(t.html() t.children(":first")样品 <a title="alpc" href="#">alpc</a>