简体   繁体   English

jQuery:如果DIV包含('text'),. attr是否为href?

[英]jQuery: .attr to href if DIV contains('text')?

(Twitter) I am trying but not sure how to use this to .attr at beginning of links/A/href , if the element selected contains certain words: (Twitter)我正在尝试但不确定如何在links / A / href的开头使用此属性.attr ,如果所选元素包含某些单词:

 $(".tweet:contains('government','Anonymous')").each(function(){
   $old_url = $(this).attr('href');
   $new_url = 'http://blankrefer.com/?'+$old_url;
   $(this).attr('href',$new_url);
  });

This works but without text detection: 这有效,但没有文本检测:

$("A").each(function(){
   $old_url = $(this).attr('href');
   $new_url = 'http://blankrefer.com/?'+$old_url;
   $(this).attr('href',$new_url);
});

You cannot pass multiple search strings to :contains , see this for more information. 你可以不通过多个搜索字符串:contains ,看到这个以获取更多信息。 You can repeat the selector with another search string using comma separator as shown below. 您可以使用逗号分隔符将选择器与另一个搜索字符串重复,如下所示。

 $(function(){ $(".tweet:contains('government'),.tweet:contains('Anonymous')").each(function(){ $old_url = $(this).attr('href'); alert($old_url); $new_url = 'http://blankrefer.com/?'+$old_url; $(this).attr('href',$new_url); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <a class="tweet" href="gov">government one</a> <a class="tweet" href="ano">Anonymous one</a> <a class="tweet" href="gov2">govern two</a> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM