gpt4 book ai didi

javascript - 停止将某个类应用于事件目标的父级

转载 作者:行者123 更新时间:2023-12-03 05:18:21 25 4
gpt4 key购买 nike

我正在练习事件委托(delegate),我偶然发现了这一点。我在 div 中的文章标签内设置了 4 个段落。我想应用一个 .green 类,仅当我单击它时才更改每个段落的背景。然而,绿色类仅适用于父级(文章)和祖 parent (div)。请告诉我我的代码有什么问题。

function paintParagraph(e) {
if (!e) {
e = window.event;
}
var target, parent;
target = e.target || e.srcElement;
parent = target.parentNode;
parent.className = 'green';
e.stopPropagation();

}

var paragraphList = document.querySelector('#p-list');
paragraphList.addEventListener('click', function(e) {

paintParagraph(e);

}
, false);
    p {
padding: 10px;
margin: 20px;
border: 2px solid;
line-height: 1.5em;
letter-spacing: 0.2em;
text-align: center;
}

.green {
background-color: green;
}
<div id="main">
<article id="p-list">
<p class="para">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vitae perferendis culpa velit excepturi iste reprehenderit quaerat debitis repudiandae itaque beatae. Numquam dolorum ducimus iusto temporibus facilis error possimus amet, illo.</p
><p class="para2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vitae perferendis culpa velit excepturi iste reprehenderit quaerat debitis repudiandae itaque beatae. Numquam dolorum ducimus iusto temporibus facilis error possimus amet, illo.</p
><p class="para3">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vitae perferendis culpa velit excepturi iste reprehenderit quaerat debitis repudiandae itaque beatae. Numquam dolorum ducimus iusto temporibus facilis error possimus amet, illo.</p
><p class="para4">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vitae perferendis culpa velit excepturi iste reprehenderit quaerat debitis repudiandae itaque beatae. Numquam dolorum ducimus iusto temporibus facilis error possimus amet, illo.</p
></article>
</div>

最佳答案

您应该检查单击的目标是否为 p,然后才继续下一步。

if(target.nodeName !== "P") return false;

现在您将只有 p 元素,因为目标在其上应用了类。

 target.className = 'green';

function color(elem) {
if (elem.nodeName == "P") {
elem.className = 'green';
}

if (elem.parentNode !== null) {
color(elem.parentNode)
}
}



function paintParagraph(e) {
if (!e) {
e = window.object;
}
var target, parent;
target = e.target || e.srcElement;
color(target)

}

var paragraphList = document.querySelector('#p-list');
paragraphList.addEventListener('click', function(e) {

paintParagraph(e);

}, false);
p {
padding: 10px;
margin: 20px;
border: 2px solid;
line-height: 1.5em;
letter-spacing: 0.2em;
text-align: center;
}
.green {
background-color: green;
}
<div id="main">
<article id="p-list">
<p class="para">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vitae perferendis culpa velit excepturi iste reprehenderit quaerat debitis repudiandae itaque beatae. Numquam dolorum ducimus iusto temporibus facilis error possimus amet, illo.</p>
<p class="para2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vitae perferendis culpa velit excepturi iste reprehenderit quaerat debitis repudiandae itaque beatae. Numquam dolorum ducimus iusto temporibus facilis error possimus amet, illo.</p>
<p class="para3">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vitae perferendis culpa velit excepturi iste reprehenderit quaerat debitis repudiandae itaque beatae. Numquam dolorum ducimus iusto temporibus facilis error possimus amet, illo.</p>
<p class="para4">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vitae perferendis culpa <span> HELLO</span>velit excepturi iste reprehenderit quaerat debitis repudiandae itaque beatae. Numquam dolorum ducimus iusto temporibus facilis error possimus amet,
illo.</p>
</article>
</div>

关于javascript - 停止将某个类应用于事件目标的父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41511236/

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