如何通过他的child使用javascript改变parent元素属性?
How to change parent element properties by his child using javascript?
我想知道如何访问没有任何标识符的 parent 元素。通常我想执行以下操作:
<td>
<a title="mySweetTitle"/>
</td>
使用他的 "a" child 访问 "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
你要找的是$(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>
我想知道如何访问没有任何标识符的 parent 元素。通常我想执行以下操作:
<td>
<a title="mySweetTitle"/>
</td>
使用他的 "a" child 访问 "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
你要找的是$(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>