定位$(this)中的特定元素

时间:2018-04-05 12:32:22

标签: javascript jquery html

我需要将id popMessage仅在第一类pop

中添加

这是jquery代码

for i in range(10:2) // iterate the Userprofile_df for all profile features,then 9,then 8...

Similaruserdf[] = df[subset when all i features are same and income is >]
if(Similaruserdf.length==0)//no such users with all features same 
continue loop reduce number of features to match on 
else
return Similaruserdf[]

这是html正文

$(".list-group-item").mouseenter(function(){
  if($(this).textContent = "Property damage" )
  {
    $(".pop").append(" <div id='popMessage'><b>Appended text</b><img id='theImg' src='https://d30y9cdsu7xlg0.cloudfront.net/png/181436-200.png'/></div>")
  }
});

li的内容是从ng-repeat(angularjs)

中提取的

3 个答案:

答案 0 :(得分:0)

您可以使用:first选择器附加到第一个选项卡,然后您可以进行验证以避免多次附加...就像示例一样。

$(".list-group-item:first").mouseenter(function(){
  if(!$(this).find('#popMessage').length)
  {
    $(".pop:first").append(" <div id='popMessage'><b>Appended text</b><img id='theImg' src='https://d30y9cdsu7xlg0.cloudfront.net/png/181436-200.png'/></div>")
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="list-group-item">Property damage<div class= "pop"></div></li>
<li class="list-group-item">Bodily injury<div class= "pop"></div></li>

答案 1 :(得分:0)

您的代码中存在多个问题。您需要做的是修复这些,以便您可以定位正确的元素。您在if语句中有一个作业。您还在寻找不属于jQuery的textContent。而且您正在选择所有.pop元素,而不是当前正在悬停的元素。

&#13;
&#13;
$(".list-group-item").mouseenter(function(){
  var li = $(this)  //reference the element
  if(li.text() === "Property damage" ) {  //notice here it uses text and correct comparison
    li.find(".pop").append(" <div id='popMessage'><b>Appended text</b><img id='theImg' src='https://d30y9cdsu7xlg0.cloudfront.net/png/181436-200.png'/></div>")  //now we find the pop element in the li that is hovered
  }
});

$(".list-group-item").mouseleave(function(){
  $(this).find(".pop").empty()
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="list-group-item">Property damage<div class= "pop"></div></li>
<li class="list-group-item">Bodily injury<div class= "pop"></div></li>
</ul>
&#13;
&#13;
&#13;

现在最终使用文本是一个坏主意。你最好使用类,数据属性或id。

答案 2 :(得分:-2)

$(this).find('.pop').append(" <div id='popMessage'><b>Appended text</b><img id='theImg' src='https://d30y9cdsu7xlg0.cloudfront.net/png/181436-200.png'/></div>")

试试吧

相关问题