如何选择页面上具有与所有其他按钮相同类的一个按钮?

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

How do I select one button on a page which has same classes as all other buttons?

问题

如何在点击编辑按钮时为其添加事件侦听器,以使它们编辑不同的部分,如果所有编辑按钮的类和HTML代码都相同
英文:

How do I add event listeners on click to the edit button and make them edit different sections if the classes and HTML code is the same for all edit buttons

<div className="top-row">
  <div className="left-header">ABOUT ME</div>
  <div className="right-header">Edit</div>
</div>
<div className="bottom-row">

...

<div className="top-row">
  <div className="left-header">PLEASE UPDATE YOUR LINKS</div>
  <div className="right-header">Edit</div>
</div>
<div className="bottom-row">

Screen layout for reference
如何选择页面上具有与所有其他按钮相同类的一个按钮?

getElementByClassName() does not seem to work

答案1

得分: 1

你需要使用一个基于位置的选择器来限定它。

此外,你的HTML是不正确的,你应该使用 class 而不是 classNameclassName 用于在JavaScript中访问应用于元素的所有类的列表。在HTML中,你只需使用 class 属性。

请参见下面的注释:

<div class="wrapper">
  <div class="top-row">
    <div class="left-header">ABOUT ME</div>
    <div class="right-header">Edit</div>
  </div>
  <div class="top-row">
    <div class="left-header">EMAIL</div>
    <div class="right-header">Edit</div>
  </div>
  <div class="top-row">
    <div class="left-header">LINKS</div>
    <div class="right-header">Edit</div>
  </div>
</div>
英文:

You'd need to use a selector that is position based to qualify it.

Also, your HTML is incorrect, you should be using class not className. className is used in JavaScript to access a list of all the classes applied to an element. In HTML, you just use the class attribute.

See comments below:

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

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

// Set up a single event handler on a common ancestor of 
// the elements you wish to handle
document.querySelector(&quot;.wrapper&quot;).addEventListener(&quot;click&quot;, function (event){
  // Determine if the event originated at an element you care about
  if(event.target.classList.contains(&quot;right-header&quot;)){
    // Handle the event based on the text of the
    // clicked element&#39;s immediate previous element sibling
    switch (event.target.closest(&quot;div.top-row&quot;).querySelector(&quot;div.left-header&quot;).textContent.toLowerCase()) {
        case &quot;about me&quot;:
          console.log(&quot;You clicked the About Me edit.&quot;);
          break;
        case &quot;email&quot;:
          console.log(&quot;You clicked the Email edit.&quot;);
          break;
        case &quot;links&quot;:
          console.log(&quot;You clicked the Links edit.&quot;);
          break;
     }
  }
});

<!-- language: lang-css -->

.right-header {
  background-color:#e0e0e0;
  padding:5px;
  border:1px solid #808080;
  display:inline-block;
  cursor:pointer;
}

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

&lt;div class=&quot;wrapper&quot;&gt;
  &lt;div class=&quot;top-row&quot;&gt;
    &lt;div class=&quot;left-header&quot;&gt;ABOUT ME&lt;/div&gt;
    &lt;div class=&quot;right-header&quot;&gt;Edit&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;top-row&quot;&gt;
    &lt;div class=&quot;left-header&quot;&gt;EMAIL&lt;/div&gt;
    &lt;div class=&quot;right-header&quot;&gt;Edit&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;top-row&quot;&gt;
    &lt;div class=&quot;left-header&quot;&gt;LINKS&lt;/div&gt;
    &lt;div class=&quot;right-header&quot;&gt;Edit&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

以下是代码的翻译部分:

<div class="container container-a">
  <div class="a-row top-row">
    <div class="left-header">关于我</div>
    <div class="right-header" data-event-action="editLinks">编辑</div>
  </div>
  <div class="a-row">
    <div class="left-header">送弗雷迪回家</div>
    <div class="right-header" data-event-action="sendFreddy">编辑</div>
  </div>
  <div class="a-row">
    <div class="left-header">晚餐吃鱼</div>
    <div class="right-header" data-event-action="fish">编辑</div>
  </div>
  <div class="a-row bottom-row">
    <div class="left-header">请更新您的链接</div>
    <div class="right-header" data-event-action="editAbout">编辑</div>
  </div>
</div>
<div class="container container-b">
  <div class="a-row top-row">
    <div class="left-header">关于我</div>
    <div class="right-header" data-event-action="another" data-type="去买啤酒">编辑</div>
  </div>
  <div class="a-row">
    <div class="left-header">什么?点击查看!</div>
    <div class="right-header" data-event-action="another" data-type="鳟鱼">编辑</div>
  </div>
  <div class="a-row">
    <div class="left-header">晚餐吃鱼</div>
    <div class="right-header" data-event-action="another" data-type="鲈鱼">编辑</div>
  </div>
  <div class="a-row bottom-row">
    <div class="left-header">我什么都不做!</div>
    <div class="right-header" data-event-action="whocares">编辑</div>
  </div>
</div>

以上是HTML代码的翻译部分。

英文:

A number of ways to do this based on element classes, position of the type, based on data attributes etc.

Here are a few examples.

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

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

function editLinks(event) {
console.log(&quot;Get that link edited!&quot;);
}
function editAbout(event) {
console.log(&quot;Edit About!&quot;);
}
function lastHandler(event) {
console.log(&quot;lastHandler woot!&quot;);
}
function clickAnotherEventHander(event) {
console.clear();
console.log(`We have to: ${this.dataset.type} `);
}
function clickEventHander(event) {
const action = event.target.dataset.eventAction;
switch (action) {
case &#39;editLinks&#39;:
editLinks(event)
break;
case &#39;editAbout&#39;:
editAbout(event)
break;
default:
console.log(`Sorry, we are out of ${action}.`);
}
}
const edits = document.querySelector(&#39;.container-a&#39;).querySelectorAll(&#39;.right-header&#39;);
edits.forEach(edt =&gt; {
edt.addEventListener(&#39;click&#39;, clickEventHander, false);
});
const nextup = document.querySelector(&#39;.container-b&#39;)
.querySelectorAll(&#39;[data-event-action=&quot;another&quot;]&#39;);
nextup.forEach(edt =&gt; {
edt.addEventListener(&#39;click&#39;, clickAnotherEventHander, false);
});
const lastly = document.querySelector(&#39;.container-b&#39;)
.querySelectorAll(&#39;.right-header:nth-last-of-type(1)&#39;);
lastly.forEach(edt =&gt; {
console.log(&#39;geter done&#39;);
edt.addEventListener(&#39;click&#39;, lastHandler, false);
});

<!-- language: lang-css -->

body {
font-size: 16px;
margin: 0;
padding: 0;
box-sizing: border-box;
display: grid;
place-items: center;
gap: 1rem;
}
.container {
display: grid;
border: double #00FF00 3px;
}
.right-header {
border-radius: 1rem;
background-color: #FFA500;
width: 5rem;
text-align: center;
padding: 0.25rem;
}
.a-row {
border: solid 1px #0000FF;
padding: 1rem;
}

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

&lt;div class=&quot;container container-a&quot;&gt;
&lt;div class=&quot;a-row top-row&quot;&gt;
&lt;div class=&quot;left-header&quot;&gt;ABOUT ME&lt;/div&gt;
&lt;div class=&quot;right-header&quot; data-event-action=&quot;editLinks&quot;&gt;Edit&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;a-row&quot;&gt;
&lt;div class=&quot;left-header&quot;&gt;Send Freddy home&lt;/div&gt;
&lt;div class=&quot;right-header&quot; data-event-action=&quot;sendFreddy&quot;&gt;Edit&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;a-row&quot;&gt;
&lt;div class=&quot;left-header&quot;&gt;Fish for dinner&lt;/div&gt;
&lt;div class=&quot;right-header&quot; data-event-action=&quot;fish&quot;&gt;Edit&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;a-row bottom-row&quot;&gt;
&lt;div class=&quot;left-header&quot;&gt;PLEASE UPDATE YOUR LINKS&lt;/div&gt;
&lt;div class=&quot;right-header&quot; data-event-action=&quot;editAbout&quot;&gt;Edit&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;container container-b&quot;&gt;
&lt;div class=&quot;a-row top-row&quot;&gt;
&lt;div class=&quot;left-header&quot;&gt;ABOUT ME&lt;/div&gt;
&lt;div class=&quot;right-header&quot; data-event-action=&quot;another&quot; data-type=&quot;Get beer!&quot;&gt;Edit&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;a-row&quot;&gt;
&lt;div class=&quot;left-header&quot;&gt;What? Click and see!&lt;/div&gt;
&lt;div class=&quot;right-header&quot; data-event-action=&quot;another&quot; data-type=&quot;walleye&quot;&gt;Edit&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;a-row&quot;&gt;
&lt;div class=&quot;left-header&quot;&gt;Fish for dinner&lt;/div&gt;
&lt;div class=&quot;right-header&quot; data-event-action=&quot;another&quot; data-type=&quot;bass&quot;&gt;Edit&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;a-row bottom-row&quot;&gt;
&lt;div class=&quot;left-header&quot;&gt;I do nothing!&lt;/div&gt;
&lt;div class=&quot;right-header&quot; data-event-action=&quot;whocares&quot;&gt;Edit&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

我在CodeSandbox中解决了这个问题。

https://codesandbox.io/s/boring-edison-hii8rb?file=/index.html

您可以使用querySelectorAll来捕捉所有具有此类的元素,并使用forEach来对它们进行迭代。

我发现了一个错误,您使用了class作为className,在React中使用class,而在HTML中我们只使用class,因为这样您无法使用getElementsByClassName函数来获取它。

<div class="top-row">
  <div class="left-header">关于我</div>
  <div class="right-header">编辑</div>
</div>
<div />

<div class="top-row">
  <div class="left-header">请更新您的链接</div>
  <div class="right-header">编辑</div>
</div>
<div />

<script>
  const editBtn = document.querySelectorAll(".right-header");
  editBtn.forEach((element) => {
    console.log(element);
  });
</script>

希望这对您有所帮助。

英文:

I resolved this in codesandbox.

https://codesandbox.io/s/boring-edison-hii8rb?file=/index.html

you can use querySelectorAll to catch all elements if this class, and use forEach to iterate them.

I found an error, you use class as className, this is used in React, in HTML we use only class, because this you can't get with getElementsByClassName function.

&lt;div class=&quot;top-row&quot;&gt;
&lt;div class=&quot;left-header&quot;&gt;ABOUT ME&lt;/div&gt;
&lt;div class=&quot;right-header&quot;&gt;Edit&lt;/div&gt;
&lt;/div&gt;
&lt;div /&gt;
&lt;div class=&quot;top-row&quot;&gt;
&lt;div class=&quot;left-header&quot;&gt;PLEASE UPDATE YOUR LINKS&lt;/div&gt;
&lt;div class=&quot;right-header&quot;&gt;Edit&lt;/div&gt;
&lt;/div&gt;
&lt;div /&gt;
&lt;script&gt;
const editBtn = document.querySelectorAll(&quot;.right-header&quot;);
editBtn.forEach((element) =&gt; {
console.log(element);
});
&lt;/script&gt;

答案4

得分: 0

以下是翻译好的部分:

你的问题有点令人困惑,因为你要求根据网站的HTML来重新创建其功能,但你给这个问题加了一个React的标签,所以如果你只是使用HTML来编写这个网站,那么@JoSSte是对的,你可以使用唯一的ID来选择需要的元素。你还可以使用getElementsByClassName,并为结果数组中的每个项编写单独的addEventListener回调函数。代码会类似这样:

let editButtons = document.getElementsByClassName("right-header");

editButtons[0].addEventListener("click", (event) => {输入功能代码});

如果你想要在React中为每个"编辑"按钮执行不同的操作,但你尝试克隆的网站并没有为每个按钮使用唯一的ID,那么他们可能是使用props将每个操作的信息传递到多次调用的组件中。

例如,你可以在你的组件中做这样的事情:

//在父组件内:

const editAboutMe = () => {
   { 在这里放入所需的功能 }
}    

return (
    <>
      <MenuItem title="ABOUT ME" edit={editAboutMe}/>
    </>
)

//在子组件内:

return (
    <>
      <div>{title}</div>
      <div onClick={edit}>EDIT</div>
    </>
)

代码有点零散,但希望这对你有所帮助!

英文:

Your question is a bit confusing, because you're asking for help re-creating the functionality of a site based on its HTML, but you tagged this question with React, so if you're just using HTML to code this site, then @JoSSte is right, you can just use unique ids to select the elements in question. You could also use getElementsByClassName and write individual addEventListener callbacks for each item in the resultant array. That would look something like this:

let editButtons = document.getElementsByClassName(&quot;right-header&quot;);
editButtons[0].addEventListener(&quot;click&quot;, (event) =&gt; {Enter function here});

If you're looking to execute different actions for each Edit button using React, but the site that you're trying to clone isn't utilizing unique ids for each one, then it's likely they're probably using props to pass the information for each action into a component that's being called multiple times.

For example, you could do something like this in your components:

//INSIDE PARENT COMPONENT: 

  const editAboutMe = () =&gt; {
   { Desired functionality here }
  }    

  return (
    &lt;&gt;
      &lt;MenuItem title=&quot;ABOUT ME&quot; edit={editAboutMe}/&gt;
    &lt;/&gt;
  ) 

//INSIDE CHILD COMPONENT:
  
  return (
    &lt;&gt;
      &lt;div&gt;{title}&lt;div&gt;
      &lt;div onClick={edit}&gt;EDIT&lt;/div&gt;
    &lt;/&gt;
  )

Code is a bit choppy, but hopefully that helps!

huangapple
  • 本文由 发表于 2023年4月7日 03:40:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953202.html
匿名

发表评论

匿名网友

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

确定