gpt4 book ai didi

javascript - 如何找到脚本标签的父级

转载 作者:数据小太阳 更新时间:2023-10-29 05:13:35 25 4
gpt4 key购买 nike

如何找到相对于脚本位置的元素?例如,在下面的例子中,我希望 getSelfParent 返回 $('button') 元素

<button>
<script>
getSelfParent().click(function(){...});
</script>
</button>

$(this).parent() 无效。

编辑:

我想避免:

  • 在任何地方添加 ID
  • 每次寻找self 元素时都遍历整棵树

最佳答案

@Jakub M: this is the way I want to create buttons in my php.

尝试将 HTML|SCRIPT 输出生成为:

<button>
<script id='RandomOrUniqueIdValue'>
var script=document.getElementById('RandomOrUniqueIdValue');
// var script=$('#RandomOrUniqueIdValue');
</script>
</button>

关于javascript - 如何找到脚本标签的父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7400248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com