请考虑以下
HTML和
Javascript.在脚本中,我正在用p标签替换标签.我期望alert()函数返回p标记的内容,但它返回原始标记的内容,该标记不再存在.
我如何引用新元素?
HTML:
@H_301_6@<a href="">This is a link</a>使用Javascript:
@H_301_6@$(document).ready(function() { $("a").each(function() { $(this).replaceWith('<p>New Paragraph</p>'); alert($(this).text()); }); });解决方法
你不能直接使用
.replaceWith()
,但你可以单独创建它.试试这个:
@H_301_6@$(document).ready(function() {
$("a").each(function() {
var p = $('<p>New Paragraph</p>');
$(this).replaceWith(p);
alert(p.text());
});
});