How to get the closest element id attribute

非 Y 不嫁゛ 提交于 2019-12-20 07:10:30

问题


I am trying to find the closest element that has id attribute and get the id. The element could be <p>, <div> or other elements.

I have tried

element=$('#test').closest('div[id]').attr('id');

but it only get the id if the element is a div. I want to be more universal. Are there anyways to do this? Thanks so much!


回答1:


You can get rid of the div entirely and look for all elements that have an id attribute:

$('#test').closest('[id]').attr('id');


来源:https://stackoverflow.com/questions/14824803/how-to-get-the-closest-element-id-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!