簡體   English   中英

JavaScript查找父級或同級

[英]Javascript finding parent or sibling

我有這個html代碼:

<div class="comment" id="7" >
  <div style="display:inline-block">
    <img style="width:64px;height:64px" src="http://origin.black-marketplace.net/content/images/users/1.jpg"><br>
    <div class="rating" style="background-position:0px -10px" title="1 stelle su 5"></div>
  </div>
  <div style="display:inline-block;float:right;width:172px;height:75px">
    <b>Xriuk</b>
    <div style="font-size:10px;color:#BEBEBE;line-height:10px">ha comprato</div>
    <span class="ellips games">mkx brteshtnx</span>
  </div>
  <span style="font-size:12px;height:50px;width:236px;display:block" class="ellips">sdjchsdui edi0ufwuèè+eè+è+èàùiek ci0hxjomwui9vjko'asdhvfyu8rk cxi0ehfuioweju9cwej icjnweuceioncuiasn cu9wecji0wejucm vuiom fiwefdoeqr hg wgtehwhwtwghrh</span>
  <a class="url" style="float:right;font-size:11px;display:none" href="7">Continua a leggere -&gt;</a>
</div>

基本上,如果超出框的大小,則所有帶有類橢圓的元素都會省略 ,我要做的是顯示 如果兩個范圍中的任何一個包含字符串“ ... ”。

我已經做了:

$(".ellips").ellipsis();
var text = document.getElementsByClassName("ellips");
if(text){
  for(var i = 0; i < text.length; i++){
    if(text[i].innerHTML.indexOf("...") != "-1"){
      ***HERE***
    }
  }
}

***HERE***我需要放置一個代碼,該代碼返回頂級父元素“ .comment ”的子元素“ #url ”(頂級父元素必須與所選的當前“ .ellips ”相對應)。
有什么幫助嗎?

謝謝!

嘗試

$(".ellips").ellipsis();
$('.comment .ellips').each(function () {
    if (this.innerHTML.indexOf("...") != "-1") {
        var $url = $(this).closest('.comment').find('.url');
        //here $url refers the element with url class under the same .comment element
    }
})

嘗試這個:

  $(text[i]).closest('.comment').find('a.url')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM