繁体   English   中英

如何使用javascript改变他的孩子的父元素属性?

[英]How to change parent element properties by his child using javascript?

我想知道如何在没有任何标识符的情况下访问父元素。 通常我想做以下事情:

 <td> <a title="mySweetTitle"/> </td> 

使用他的“a”孩子访问“td”来修改他的属性。

也许这可以帮助你:

$("a").bind("click" , function(){
    var parent = $(this).parent();
});

你应该使用parentElement属性https://developer.mozilla.org/en/docs/Web/API/Node/parentElement

例:

document.getElementById('your_id').parentElement

在你的情况下你可以使用

document.getElementsByTagName('a')[0].parentElement

你正在寻找什么ist $(this).parent()看看我的例子我希望它有所帮助

 $(document).ready(function() { $('.test').on('click', function() { $(this).parent().append("<button>test2</button>"); }); }); 
 <!doctype HTML> <html> <head> <title>Test Umgebung</title> <meta charset="UTF-8"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> </head> <body> <div> <button class="test">test</button> </div> </body> </html> 

暂无
暂无

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

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