(Twitter)我正在尝试但不确定如何在 links / A / href 的开头使用此 .attr ,如果所选元素包含某些字词:
$json = file_get_contents('http://higconsolidated.com/json.json');
$obj = json_decode($json);
var_dump($obj);
这有效,但没有文字检测:
$(".tweet:contains('government','Anonymous')").each(function(){
$old_url = $(this).attr('href');
$new_url = 'http://blankrefer.com/?'+$old_url;
$(this).attr('href',$new_url);
});
答案 0 :(得分:0)
您无法将多个搜索字符串传递给:contains
,有关详细信息,请参阅this。您可以使用逗号分隔符使用另一个搜索字符串重复选择器,如下所示。
$(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>