gpt4 book ai didi

javascript - jQuery - 点击 * 选择器触发太多次

转载 作者:行者123 更新时间:2023-11-28 16:48:40 25 4
gpt4 key购买 nike

我想记录用户点击的每个元素的完整css路径,我关注this答案是创建一个函数来获取被单击元素的 css 路径

$('body').on('click',"*",function(){
var rightArrowParents = [];
$(this).parents().addBack().not('html').each(function(e) {
var entry = this.tagName.toLowerCase();

if($(this).attr('id')){
entry += "#" + $(this).attr('id');
}

if (this.className) {
entry += "." + this.className.replace(/ /g, '.');
}

rightArrowParents.push(entry);
});
console.log(rightArrowParents.join(" > "));
})

但是我注意到,当我单击由多个元素包裹的元素时,此函数被调用很多次,

例如,当我点击 h1 时:

body > div.sample1.sample2 > div.sample3.sample4 > h1.test1.test2.test3

body > div.sample1.sample2 > div.sample3.sample4

body > div.sample1.sample2

当我添加return false时在console.log之后,它只会输出h1的完整css路径,这是预期的结果,但如果我这样做,某些元素将无法正常工作,例如<a>不会重定向到另一个网站

以下是我尝试过的:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<title>Document</title>
</head>
<body>
<div class="sample1 sample2">
<div class="sample3 sample4">
<h1 class="test1 test2 test3">some example text</h1>
</div>
</div>
<div>
<h1 class="test1 test2 test3">some sample text</h1>
</div>
<div class="sample1 sample2">
<div class="sample3 sample4">
<a class="test1 test2 test3" href="https://www.google.com/">henlo</a>
</div>
</div>
<script>
jQuery(function($){
$('body').on('click',"*",function(){
var rightArrowParents = [];
$(this).parents().addBack().not('html').each(function(e) {
var entry = this.tagName.toLowerCase();

if($(this).attr('id')){
entry += "#" + $(this).attr('id');
}

if (this.className) {
entry += "." + this.className.replace(/ /g, '.');
}

rightArrowParents.push(entry);
});
console.log(rightArrowParents.join(" > "));
//can't do this, since it will make some element malfunction
//return false
})
})
</script>
</body>
</html>

我应该怎样做才能使 jQuery 正常工作?

最佳答案

如果我清楚地理解你的问题,你需要做的就是向你的函数 function(e) 添加一个事件参数,然后使用 e.stopPropagation() end 以防止它通过父元素的事件处理程序。

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<title>Document</title>
</head>

<body>
<div class="sample1 sample2">
<div class="sample3 sample4">
<h1 class="test1 test2 test3">some example text</h1>
</div>
</div>
<div>
<h1 class="test1 test2 test3">some sample text</h1>
</div>
<div class="sample1 sample2">
<div class="sample3 sample4">
<a class="test1 test2 test3" href="https://www.google.com/">henlo</a>
</div>
</div>
<script>
jQuery(function($) {
$('body').on('click', "*", function(e) {
var rightArrowParents = [];
$(this).parents().addBack().not('html').each(function() {
var entry = this.tagName.toLowerCase();

if ($(this).attr('id')) {
entry += "#" + $(this).attr('id');
}

if (this.className) {
entry += "." + this.className.replace(/ /g, '.');
}

rightArrowParents.push(entry);
});
console.log(rightArrowParents.join(" > "));

e.stopPropagation();
})
})
</script>
</body>

</html>

关于javascript - jQuery - 点击 * 选择器触发太多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60222560/

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