Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.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从元素中获取属性的id?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何使用jquery从元素中获取属性的id?

Javascript 如何使用jquery从元素中获取属性的id?,javascript,jquery,html,Javascript,Jquery,Html,我编写了这个小jquery代码段,但是我想获取li元素的id,这样我就可以将这些数据发布到ajax调用中。这是我的脚本 $('#list li a').on('click', function(e) { var id = ? //need to get li id e.preventDefault(); $.ajax({ type: "POST", url: "unfollow.php", da

我编写了这个小jquery代码段,但是我想获取li元素的id,这样我就可以将这些数据发布到ajax调用中。这是我的脚本

 $('#list li a').on('click', function(e) {

        var id = ? //need to get li id
        e.preventDefault();

        $.ajax({
        type: "POST",
        url: "unfollow.php",
        data: id,
        success: function(){
            $(this).parent().hide();
        $('.updateNumber').html(function(){
            return parseInt($(this).text(),10)+1;
          });
        }
         });
       });
    });
这是一个示例html列表:

<ul id="list">
    <li id="list_1" class="list"><a href="">hide</a></li>
</ul>
谢谢:

 $('#list li a').on('click', function(e) {
    var id = $(this).parent("li").attr("id");
    e.preventDefault();

    $.ajax({
    type: "POST",
    url: "unfollow.php",
    data: id,
    success: function(){
        $(this).parent().hide();
    $('.updateNumber').html(function(){
        return parseInt($(this).text(),10)+1;
      });
    }
     });
   });
});
看见 看见 看见 看见 由于该方法:

由于该方法:


您可以使用纯javascript,无需额外调用jQuery

var id = this.parentNode.id 
在这里拉小提琴

编辑-仅获取数字

var id = this.parentNode.id.replace('list_', '')

您可以使用纯javascript,无需额外调用jQuery

var id = this.parentNode.id 
在这里拉小提琴

编辑-仅获取数字

var id = this.parentNode.id.replace('list_', '')
这比jQuery的速度至少快8倍。不要将jQuery用于如此简单的事情

这比jQuery的速度至少快8倍。不要将jQuery用于如此简单的事情。

在jsFiddle.net上检查这一点。您需要将.parent与$this一起使用

HTML

这将给出1或2作为id。 希望这对您有所帮助。

请在jsFiddle.net上查看。您需要将.parent与$this一起使用

HTML

这将给出1或2作为id。 希望这对您有所帮助。

有关使用列表的数量

有关列表的编号,\n请使用


他想要li的id,选择器在a上,所以我宁愿说:var id=$this.parent.attrid;如何从列表中删除列表?我只希望它显示数字1!对不起,我没有澄清!!!您可以在id上执行.replace。只需使用javascript的replace函数替换它即可。变量id是一个字符串,因此您可以使id=id.replacelist_389;,->@MidnightCoderhe想要li的id,选择器在a上,所以我宁愿说:var id=$this.parent.attrid;如何从列表中删除列表?我只希望它显示数字1!对不起,我没有澄清!!!您可以在id上执行.replace。只需使用javascript的replace函数替换它即可。变量id是一个字符串,因此您可以使id=id.replacelist_389;,->@MidNightCode在这种情况下,我认为您可以避免使用this.parentNode进行所有这些调用的开销。id@Nicola,您是对的,因为元素是提问者标记中元素的直接子元素。但是他使用的选择器是list li a,而不是list li>a,所以我使用了最接近安全的方法:是的,为了安全,你是绝对正确的,这是性能与便携性的问题!:在这种情况下,我认为您可以避免使用this.parentNode进行所有这些调用的开销。id@Nicola,您是对的,因为元素是提问者标记中元素的直接子元素。但是他使用的选择器是list li a,而不是list li>a,所以我使用了最接近安全的方法:是的,为了安全,你是绝对正确的,这是性能与便携性的问题!:谢谢你的回答,但我不知道要保留什么,只保留数字1,对不起,我没有澄清!!var id_number=this.parentNode.id.match/\d+/[0]@middnightcoder然后使用this.parentNode.id.replace'list_';谢谢你的回答,但我不知道要保留什么,只保留数字1,对不起,我没有澄清!!var id_number=this.parentNode.id.match/\d+/[0]@middnightcoder然后使用this.parentNode.id.replace'list_';
<ul id="list">
  <li id="list_1">
    <a href="#">Test Anchor One</a>
  </li>
  <li id="list_2">
    <a  href="#">Test Anchor Two</a>
  </li>
</ul>​
$('#list li a').on('click', function(e) {
    var id = $(this).parent().attr("id").replace("list_", "");
   alert(id);
});​
var id = this.parentNode.id;
var number = id.substring(5);