jquery – 在.replaceWith()之后使用$(this)

前端之家收集整理的这篇文章主要介绍了jquery – 在.replaceWith()之后使用$(this)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
请考虑以下 HTMLJavascript.在脚本中,我正在用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()); }); });

猜你在找的jQuery相关文章