Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery查找ul li span中的元素_Javascript_Jquery - Fatal编程技术网

Javascript jQuery查找ul li span中的元素

Javascript jQuery查找ul li span中的元素,javascript,jquery,Javascript,Jquery,我的HTML结构如下: <div title="" class="interestareascategories_chn chzn-container chzn-container-multi"> <ul id="ul_deptType" class="chzn-choices"> <li id="li_dept_1" ver="old" class="search-choice"> <span>DEV</span&g

我的HTML结构如下:

<div title="" class="interestareascategories_chn chzn-container chzn-container-multi">
    <ul id="ul_deptType" class="chzn-choices">
        <li id="li_dept_1" ver="old" class="search-choice"> <span>DEV</span>    <a rel="3" class="search-choice-close" seq="1" href="javascript:void(0)"></a>

        </li>
        <li id="li_dept_2" ver="old" class="search-choice"><span>QA</span>  <a rel="3" class="search-choice-close" seq="2" href="javascript:void(0)"></a>

        </li>
        <li id="selD7J_chzn_c_3" class="search-choice"><span>sag</span>     <a class="search-choice-close" seq="0.19415735425269753" href="javascript:void(0)"></a>

        </li>
        <li class="search-field"><span contenteditable="true" style="width: 25px;"></span>

        </li>
    </ul>
</div>

    开发人员 质量保证
  • sag

如何在锚定标记中仅获取具有
rel
属性的
seq

您可以使用下一个选择器来选择特定的
rel

var seq3 = $("a[rel='3']").attr("seq");   
要获得所有序列的总和,请使用下一步:

var sum = 0;
$.each($("a[rel='3']"), function(i, el)
                     {
                        sum += el.getAttribute("seq") | 0                         
                     })

您需要所有具有rel属性的锚点的seq属性,然后:

试着这样做:

    $("a.search-choice-close").each(function(){
        if($(this).attr('rel')) {
          alert($(this).attr('seq'));
        }
    })
或:

试试这个:

var anchor = $('a[rel][seq]'); // this only targets anchors which have these 
                               // rel and seq attributes.

您可以尝试以下方法:

$('a[rel][seq]').on('click', function () {
    alert($(this).attr('seq'));
});
您可以使用for
rel
和获取所选元素上的
seq
属性的值

代码:

演示:


您应该编写这样的脚本,您还可以将所有“seq”值存储在一个数组中。

下面是一个相当简单明了的答案:

// gets all anchors with attribute rel and place their [seq] values into an array
var values = $.map($('a[rel]'), function(el){
  return el.getAttribute('seq');
});

console.log(values); // ["1", "2"] 

您可以使用以下内容:


你试过什么?若元素有rel,你们想要什么?这不是只得到一个值吗?$(“a[rel]”)应该返回所有带有rel属性的标记,若你们想遍历它的所有sql属性,你们可以做类似$(“a[rel]”)的事情。each(function(){$(this.attr(“seq”);});我可以使用$('a[rel]').prev()在锚定标记(即sag ONLYYAH)中找到没有rel属性的span文本吗
var seq = $("a[rel]").attr("seq");
$('a[rel]').each(function(i,e){
    console.log($(this).attr('seq'))
});
jQuery(document).ready(function(){{
   var check=jQuery(".search-choice a ").attr("rel");
   if(check!="undefined")
   {
      var selVar=jQuery(this).attr("seq");
   }

})
// gets all anchors with attribute rel and place their [seq] values into an array
var values = $.map($('a[rel]'), function(el){
  return el.getAttribute('seq');
});

console.log(values); // ["1", "2"] 
$("a[rel]").map(function(i, el) { return $(el).attr("seq"); })