admin管理员组文章数量:1410737
How can I get the class name of the parent div from which a html link calls a javascript function?
In the following code example:
<div class="parent-class">
<a href="javascript:parent(this.parent)">Find parent</a>
</div>
<script>
function parent(parent) {
var theValueImLookingFor = (parent.className);
}
</script>
I would like the "theValueImLookingFor" to be "parent-class", but I keep getting "undefined".
I been trying to figure this out for almost an hour, what can I change to make this work? I understand there are more static ways to get this information, but for my specific purpose I really need to identify the div through the javascript function from which it is called.
How can I get the class name of the parent div from which a html link calls a javascript function?
In the following code example:
<div class="parent-class">
<a href="javascript:parent(this.parent)">Find parent</a>
</div>
<script>
function parent(parent) {
var theValueImLookingFor = (parent.className);
}
</script>
I would like the "theValueImLookingFor" to be "parent-class", but I keep getting "undefined".
I been trying to figure this out for almost an hour, what can I change to make this work? I understand there are more static ways to get this information, but for my specific purpose I really need to identify the div through the javascript function from which it is called.
Share Improve this question asked Oct 17, 2015 at 0:09 SVisscherSVisscher 231 gold badge1 silver badge3 bronze badges2 Answers
Reset to default 3You've tagged this with jQuery, which will be the easiest way to find the details of the parent div. Something like
<div class="parent-div">
<a id="link">Find Parent</a>
</div>
<script>
$('#link').click(function() {
alert($(this).parent().attr('class'));
});
</script>
I would avoid using JavaScript as an href value, and do something like this:
var pre = onload;
onload = function(){
if(pre)pre();
var doc = document;
function getParentClassName(childNode){
return childNode.parentNode.className;
}
console.log(getParentClassName(doc.getElementsByTagName('a')[0]));
}
本文标签:
版权声明:本文标题:jquery - How to get class name of parent div of html link from which javascript function is called? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744888307a2630618.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论