Javascript 查找包含字符串的元素并替换同级元素
所以,我有一个站点,我只有类来隔离我想要编辑的元素。我需要在html中找到一个包含特定字符串的元素,并替换它的一个同级html。不管怎样,这是我得到的代码:Javascript 查找包含字符串的元素并替换同级元素,javascript,jquery,html,Javascript,Jquery,Html,所以,我有一个站点,我只有类来隔离我想要编辑的元素。我需要在html中找到一个包含特定字符串的元素,并替换它的一个同级html。不管怎样,这是我得到的代码: if((".user.info").html().indexOf(vaUID[index]) >= 0) { $("a.user.info").each(function (index2, obj) { if($(this).html().indexOf(vaUID[index]) >= 0) {
if((".user.info").html().indexOf(vaUID[index]) >= 0) {
$("a.user.info").each(function (index2, obj) {
if($(this).html().indexOf(vaUID[index]) >= 0) {
$(this).siblings('.registered').html('<div class="registered"><a href=http://www.twitch.tv/'+users[index]+ '>currently streaming' + data.stream.game + '</a></div>');
}
});
}
if((.user.info”).html().indexOf(vaUID[index])>=0{
$(“a.user.info”)。每个(函数(index2,obj){
if($(this.html().indexOf(vaUID[index])>=0){
$(this.sibbines('.registered').html('');
}
});
}
以下是起始html:
<div class="user first">
<div class="element_avatar simple small "><a href="http://www.cnr-clan.com/profile/xxxxxx" data-minitooltip="username"><img src="http://assets-cloud.enjin.com/users/xxxxxx/avatar/small.xxxxxx.png"></a></div>
<div class="info">
<a href="http://www.cnr-clan.com/profile/xxxxxx" class="element_username tag-xxxxxx">username</a>
<span class="nameicons">
<!--Irrelevant Excess Code-->
</span>
<div class="registered">currently browsing</div>
</div>
<div class="clearing"><!-- --></div>
</div>
当前浏览
我想要的结果是:
<div class="user first">
<div class="element_avatar simple small "><a href="http://www.cnr-clan.com/profile/xxxxxx" data-minitooltip="username"><img src="http://assets-cloud.enjin.com/users/2219573/avatar/small.xxxxxx.png"></a></div>
<div class="info">
<a href="http://www.cnr-clan.com/profile/xxxxxx" class="element_username tag-xxxxxx">username</a>
<span class="nameicons">
<!--Irrelevant Excess Code-->
</span>
<div class="registered"><a href='http://www.twitch.tv/username'>currently streaming gamename</a></div>
</div>
<div class="clearing"><!-- --></div>
</div>
请记住,我在同一个类中有几个类似的代码块。vaUID[index]的一个示例值是xxxxxx。我建议,基于该HTML:
// selects the appropriate elements:
$('.user .info a.element_username')
// filters that collection:
.filter(function(){
// retains only those in which this assessment results as true/truthy
return $.trim($(this).text()) === 'username';
})
// searches amongst those retained-elements' sibling for those that match
// the passed-selector:
.siblings('div.registered')
// wraps the contents of those elements with the supplied HTML:
.wrapInner('<a href="http://www.twitch.tv/username"></a>');
或:
参考资料:
$(".user .info a[href='http://www.cnr-clan.com/profile/" + vaUID[index] + "']").siblings('div.registered')
.html('<a href="http://www.twitch.tv/username/' + users[index] + '">streaming ' + data.stream.game + '</a>');
$(“.user.info a[href=”http://www.cnr-clan.com/profile/“+vaUID[index]+“']”。同级('div.registered'))
.html(“”);
谢谢你让我走上正轨。我现在把这段代码作为我的js,但它似乎仍然给我带来了问题<代码>$('.user.info a.element_username').filter(function(){return$(this.html().indexOf(vaUID[index])>-1;}).slides('div').html(''代码>我想通过检查href属性上的最后几位数字来找到正确的元素,我为该属性配音了一个用户ID。此代码嵌套在一个。每一个在用户ID(vaUID[index])中迭代的语句中,例如xxxxxx)和twitch.tv用户名(users[index])
return $(this).text().toLowerCase().indexOf('username') > -1; // this is case-insensitive
$(".user .info a[href='http://www.cnr-clan.com/profile/" + vaUID[index] + "']").siblings('div.registered')
.html('<a href="http://www.twitch.tv/username/' + users[index] + '">streaming ' + data.stream.game + '</a>');