jQuery..在当前元素之前拉出一个字符串

发布于 2024-07-25 09:32:01 字数 1080 浏览 5 评论 0原文

HTML..

<div class="row">
   <div>
      <b>Title A</b> <a href="#">Link 1</a> <a href="#">Link 2</a>
   </div>
   Content A
</div>
<div class="row">
   <div>
      <b>Title B</b> <a href="#">Link 1</a> <a href="#">Link 2</a>
   </div>
   Content B
</div>
<div class="row">
   <div>
      <b>Title C</b> <a href="#">Link 1</a> <a href="#">Link 2</a>
   </div>
   Content C
</div>

jQuery..

到目前为止,我的 JS 在每个“链接 2”之后添加了一个“链接 3”链接。

$("a:contains('Link 2')").each(function(){
   $(this).after(' <a href="#">Link 3</a>');
});

对于循环中的每个项目,我将如何访问标题。

$("a:contains('Link 2')").each(function(){
   // I need to find the title string for each row. So is there a way to find elements before the $(this) identifier?
   $(this).after(' <a href="#">Link 3</a>');
});

HTML..

<div class="row">
   <div>
      <b>Title A</b> <a href="#">Link 1</a> <a href="#">Link 2</a>
   </div>
   Content A
</div>
<div class="row">
   <div>
      <b>Title B</b> <a href="#">Link 1</a> <a href="#">Link 2</a>
   </div>
   Content B
</div>
<div class="row">
   <div>
      <b>Title C</b> <a href="#">Link 1</a> <a href="#">Link 2</a>
   </div>
   Content C
</div>

jQuery..

So far my JS adds a "Link 3" link after each "Link 2"..

$("a:contains('Link 2')").each(function(){
   $(this).after(' <a href="#">Link 3</a>');
});

For each item in the loop how would I access the title.

$("a:contains('Link 2')").each(function(){
   // I need to find the title string for each row. So is there a way to find elements before the $(this) identifier?
   $(this).after(' <a href="#">Link 3</a>');
});

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

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

发布评论

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

评论(2

刘备忘录 2024-08-01 09:32:01

您可以使用 .prev() 方法 获取上一个同级。 您可以这样做,或者您可以获取链接 .parent(),然后.find() 第一个 标记。

<p>Something Here</p> <!-- This becomes red -->
<p class="pickme">Hello World</p>

$(document).ready(function(){
  $(".pickme").prev().css("color","red");
});

或者

<p>Something Here</p> <!-- this becomes green -->
<p class="pickme">Hello World</p>

$(document).ready(function(){
  $(".pickme").parent().find("p:first").css("color", "green");
});

You can get the previous sibling by using the .prev() method. You can do that, or you can get the links .parent(), and then .find() the first <b> tag.

<p>Something Here</p> <!-- This becomes red -->
<p class="pickme">Hello World</p>

$(document).ready(function(){
  $(".pickme").prev().css("color","red");
});

or

<p>Something Here</p> <!-- this becomes green -->
<p class="pickme">Hello World</p>

$(document).ready(function(){
  $(".pickme").parent().find("p:first").css("color", "green");
});
一绘本一梦想 2024-08-01 09:32:01

如果您将一个类(例如“rowTitle”)添加到包装标题的所有粗体标签中,您应该能够执行以下操作:

$("a:contains('Link 2')").each(function(){
   $(this).parent().find(".rowTitle").html
});

或者相当类似的操作。 jQuery 文档 相当全面,快速搜索应该可以帮助您找到所需内容寻找。

我不是 jQuery 专家,但我相信这已经非常接近工作了。 也许更精通此事的人可以详细说明我的答案(因为我也不介意学习)。

If you add a class, such as "rowTitle" to all of your bold tags that are wrapping your titles, you should be able to do:

$("a:contains('Link 2')").each(function(){
   $(this).parent().find(".rowTitle").html
});

Or something fairly similar. The jQuery Docs are fairly comprehensive, and a quick search there should help you find what you're looking for.

I'm not a jQuery expert, but I believe that's fairly close to working. Perhaps someone a bit more versed in the matter could elaborate on my answer (as I wouldn't mind learning as well).

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