在具有特定类的最近元素中显示文本。

huangapple go评论75阅读模式
英文:

Display text in the closest element with certain class

问题

我正在尝试在具有 .msg 类的最接近元素中显示文本,但它始终显示在具有 .msg 类的第一个元素中。

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let btn = document.querySelectorAll('button');

btn.forEach(function(i) {
  i.addEventListener('click', function() {

    let x = document.querySelector('.msg').innerHTML = i.innerHTML;
    let z = x.closest(".msg");

  });
});

<!-- language: lang-html -->
<div>
  <button class="btn">按钮1</button>
  <p class="msg"></p>
</div>
<div>
  <button class="btn">按钮2</button>
  <p class="msg"></p>
</div>
<div>
  <button class="btn">按钮3</button>
  <p class="msg"></p>
</div>
<!-- end snippet -->

如果我点击按钮1,文本应该显示在按钮1下方,如果我点击按钮2,文本应该显示在按钮2下方,依此类推...

英文:

I'm trying to display a text in the closest element with the .msg class, but it always displayed in the first element with .msg class.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let btn = document.querySelectorAll(&#39;button&#39;);

btn.forEach(function(i) {
  i.addEventListener(&#39;click&#39;, function() {

    let x = document.querySelector(&#39;.msg&#39;).innerHTML = i.innerHTML;
    let z = x.closest(&quot;.msg&quot;);

  });
});

<!-- language: lang-html -->

&lt;div&gt;
  &lt;button class=&quot;btn&quot;&gt;button 1&lt;/button&gt;
  &lt;p class=&quot;msg&quot;&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
  &lt;button class=&quot;btn&quot;&gt;button 2&lt;/button&gt;
  &lt;p class=&quot;msg&quot;&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
  &lt;button class=&quot;btn&quot;&gt;button 3&lt;/button&gt;
  &lt;p class=&quot;msg&quot;&gt;&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

在具有特定类的最近元素中显示文本。

If I click Button1, the text should be displayed underneath Button1, if I click Button2 text should be displayed underneath Button2, and so on...

答案1

得分: 1

closest
> Element 接口的 closest() 方法遍历元素及其父元素(朝向文档根部),直到找到与指定的 CSS 选择器匹配的节点。

.msg 元素位于 button 旁边,而不是在其父元素之一,因此您可以获取按钮的父元素,即 div,然后在其中使用 querySelector 来获取 .msg 元素。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let btn = document.querySelectorAll('button');

btn.forEach(function(i) {
i.addEventListener('click', function() {
const parentDiv = i.parentElement;
parentDiv.querySelector('.msg').innerHTML = i.innerHTML;

});
});

<!-- language: lang-html -->

<div>
<button class="btn">button 1</button>
<p class="msg"></p>
</div>
<div>
<button class="btn">button 2</button>
<p class="msg"></p>
</div>
<div>
<button class="btn">button 3</button>
<p class="msg"></p>
</div>

<!-- end snippet -->

英文:

closest
>The closest() method of the Element interface traverses the element and its parents (heading toward the document root) until it finds a node that matches the specified CSS selector.

The .msg element is next to the button not on of its parents, so you can get the parent element of the button which is the div, and then use querySelector into it to get the .msg element.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let btn = document.querySelectorAll(&#39;button&#39;);

btn.forEach(function(i) {
  i.addEventListener(&#39;click&#39;, function() {
    const parentDiv = i.parentElement;
    parentDiv.querySelector(&#39;.msg&#39;).innerHTML = i.innerHTML;

  });
});

<!-- language: lang-html -->

&lt;div&gt;
  &lt;button class=&quot;btn&quot;&gt;button 1&lt;/button&gt;
  &lt;p class=&quot;msg&quot;&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
  &lt;button class=&quot;btn&quot;&gt;button 2&lt;/button&gt;
  &lt;p class=&quot;msg&quot;&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
  &lt;button class=&quot;btn&quot;&gt;button 3&lt;/button&gt;
  &lt;p class=&quot;msg&quot;&gt;&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: -1

请尝试以下代码:

<script>
    let btn = document.querySelectorAll('button')

    btn.forEach(function (i, index) {
        i.addEventListener('click', function () {
            document.querySelectorAll('.msg')[index].innerHTML = i.innerHTML
        })
    })
</script>
英文:

Try this

&lt;script&gt;
	let btn = document.querySelectorAll(&#39;button&#39;)

	btn.forEach(function (i, index) {
		i.addEventListener(&#39;click&#39;, function () {
			document.querySelectorAll(&#39;.msg&#39;)[index].innerHTML = i.innerHTML
		})
	})
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年3月1日 14:54:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定