简体   繁体   English

jQuery的获取父母ID不起作用

[英]jQuery get parent id not working

I'm not great with JavaScript/jQuery and am having a lot of trouble with a very basic task. 我对JavaScript / jQuery不太满意,在执行一个非常基本的任务时遇到了很多麻烦。 I have an img that, when clicked, should give me the id of the parent div it is within. 我有一个img ,当单击它时,应该给我它所在的父div的ID。

This is the markup: 这是标记:

<div id="client-1">
    <img src="~/Content/plus.ico" alt="plus" onclick="ButtonExpandClick()" />
</div>

And here is the javascript: 这是javascript:

<script type="text/javascript">
    function ButtonExpandClick() {
        alert($(this).parent().attr("id"));
    }
</script>

Clicking the image gives me an alert that says "undefined" but I can clearly see that the div has the id of "client-1" when I inspect the page. 单击该图像会给我一个警告,提示“未定义”,但是当我检查页面时,我可以清楚地看到div的ID为“ client-1”。 I must be missing something simple here. 我一定在这里缺少一些简单的东西。 I've also tried using .closest as well as passing this into the function but no luck. 我已经使用也试过.closest以及通过this进入功能,但没有运气。 Thanks for any help! 谢谢你的帮助!

Don't use the onclick attribute for events. 不要将onclick属性用于事件。 You're using jQuery, bind the events "properly". 您正在使用jQuery,请“适当”绑定事件。

Add a class to the image(s): 向图像添加一个类:

<img src="~/Content/plus.ico" alt="plus" class="icon" />

Then bind the event: 然后绑定事件:

$(function(){
    $('.icon').click(function(){
        alert($(this).parent().attr("id"));
    });
});

If you hook up the click event using jQuery instead of doing it inline, then you'll get the this passed in automatically: 如果您使用jQuery而不是通过内联方式连接click事件,那么您将自动获得this

Note that you'd have to give the image an id or find another selector for it. 请注意,您必须为图像指定一个id或为其找到另一个选择器。

jQuery(document).ready(function() {
    jQuery("#myImg").click(ButtonExpandClick);
});

You need to pass in this 你需要传递this

onclick="ButtonExpandClick(this)"

JS JS

function ButtonExpandClick(elem) {
   alert($(elem).parent().attr("id"));
}

Also it is a bad idea to declare inline events . 声明内联事件也是一个坏主意。 Attach the event directly using javascript. 使用javascript直接附加事件。

<script>

   $(function() {
       $('#client-1 img').click(function() {
           alert($(this).parent().attr("id"));
       });
   });

</script>

You have to use jquery.click or send your element with function. 您必须使用jquery.click或将元素发送给函数。 like sendme(this). 就像sendme(this)。

You can use something like this: 您可以使用如下形式:

$(function() {
    $('img').on('click', function(e) {
            var id = this.parentNode.id; //Using javascript to access id is faster.
        alert(id);
    });
});

Example working at Jsfiddle Jsfiddle工作的示例

It's recommended put this images inside a container. 建议将此图像放在容器中。 and change this to. 并将其更改为 It's better for performance. 它对性能更好。

$(function() { $(function(){

   var images = $('#container').find('img');
   images.on('click', function(e) {
        var id = this.parentNode.id; //Using javascript to access id is faster.
        alert(id);
    });

});

Simpler still you might try this with a binding: 更简单的是,您可以尝试使用绑定:

<div id="client-1">
  <img src="~/Content/plus.ico" alt="plus"  />
</div>

<script>
  $(function() {
    $('img').on('click', function(){
      // Two ways to do the same thing
      alert(this.parentNode.id);
      alert($(this).parent()[0].id);
    });
  });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM