使用与 jQuery.html() 链接的 jQuery.not()

发布于 2024-08-02 10:21:03 字数 188 浏览 2 评论 0原文

是否可以使用与 jQuery.html() 链接的 jQuery.not()

winner.not('a').html()

其中 winner 是一个 jQuery 对象/包装集,我试图返回删除锚点的 HTML。

Is it possible to use jQuery.not() chained with jQuery.html()?

winner.not('a').html()

Where winner is a jQuery object/wrapped set, I am trying to return HTML with anchors removed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初与友歌 2024-08-09 10:21:03

.html() 将返回innerHTML - 其中将包含任何 A 标签,您可以这样做:

// clone the matched div:
var copy = winner.clone();
// remove all A tags from the DOM
copy.find("a").remove();
// get the html.
var noanchors = copy.html();

另外 - 如果您想获取 A 中的文本- 但不是 A 本身 - 你可以使用:

// for each A tag
copy.find("a").each(function() {
  //insert the text within ourselves to the document directly before us.
  $(this).before($(this).text());
  // then delete ourselves
  $(this).remove();
});

虽然如果 中有任何其他标签,这实际上可能会有点混乱 - 它应该说明这个想法。

.html() is going to return the innerHTML - which will include any A tags inside, you may be able to do something like this though:

// clone the matched div:
var copy = winner.clone();
// remove all A tags from the DOM
copy.find("a").remove();
// get the html.
var noanchors = copy.html();

Also - If you wanted to get the text within the A still - but not the A itself - you could use:

// for each A tag
copy.find("a").each(function() {
  //insert the text within ourselves to the document directly before us.
  $(this).before($(this).text());
  // then delete ourselves
  $(this).remove();
});

Although that might actually get a little messy if the <a> has any other tags within it - it should illustrate the idea.

醉酒的小男人 2024-08-09 10:21:03

这是可能的,但不会给您带来预期的结果。 not('a') 将从您的 winner 集合中过滤 a 标签,但 html() 会返回您集合中第一项的值。

因此,如果您的获胜者[div#i1 div#i2 a div#i3 a]not('a')将减少此组[div#i1 div#i2 div#i3]html() 将返回 div#i1 的 HTML 内容。

如果你想从 HTML 中删除锚点,我认为你应该首先使用 .html() 方法,然后用一些正则表达式替换返回的值,以去除锚点。

It is possible but that won't give you the result you expect. not('a') will filter a tags from your winner collection but html() will return you the value of the first item in the collection.

so if your winner has [div#i1 div#i2 a div#i3 a], not('a') will reduce this set to [div#i1 div#i2 div#i3] and html() will return HTML contents of div#i1.

If you want to remove anchors from HTML I think you should first get is using .html() method and then do a replace on the value returned with some regular expression that will strip the anchors.

我们的影子 2024-08-09 10:21:03

或者,您可以使用类似这样的方法:

var noanchors = '';
winner.not('a').each(function() { noanchors = noanchors + $(this).html(); )

获取非 a 元素的 html 的串联。然而,这可能会错过各种标签封闭项目之间的任何顶级文本。

Alternatively, you could use something like this:

var noanchors = '';
winner.not('a').each(function() { noanchors = noanchors + $(this).html(); )

to get the concatenation of the non-a elements' html. However, this may miss any top-level text in between the various tag-enclosed items.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文