Click even on button not Working if it has a span inside ( it works only when i click near border ) JS

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

Click even on button not Working if it has a span inside ( it works only when i click near border ) JS

问题

  1. <button class="anunt-edit" type="submit" data-id="@anunt.Id" id="editButton" >
  2. @Html.Partial("editSVGPartial", new {@Id="editButton", @class="svg"})
  3. </button>
  1. The Html Helper partial is for an Edit icon span,
  2. the problem is that in javascript e.target.matches("#editButton") returns true only when i click the button near border ( i think it doesnt work properly because of the span that is inside )
  1. document.addEventListener("click", e =>
  2. {
  3. var isEditButtonClicked = e.target.matches("#editButton");
  4. if ( isEditButtonClicked )
  5. {
  6. console.log("button clicked")
  7. }
  8. else
  9. {
  10. console.log("click")
  11. }
  12. })
英文:
  1. <button class="anunt-edit" type="submit" data-id="@anunt.Id" id="editButton" >
  2. @Html.Partial("editSVGPartial", new {@Id="editButton", @class="svg"})
  3. </button>

The Html Helper partial is for an Edit icon span,
the problem is that in javascript e.target.matches("#editButton") returns true only when i click the button near border ( i think it doesnt work properly because of the span that is inside )

  1. document.addEventListener("click", e =>
  2. {
  3. var isEditButtonClicked = e.target.matches("#editButton");
  4. if ( isEditButtonClicked )
  5. {
  6. console.log("button clicked")
  7. }
  8. else
  9. {
  10. console.log("click")
  11. }
  12. })

答案1

得分: 1

我将 span 的 id 更改为与其直接父级按钮的 id 相同,现在它可以正常工作。

英文:

I changed the id of the span to the same one as the id of the button that is the direct parent and now it works.

huangapple
  • 本文由 发表于 2023年2月19日 23:28:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501207.html
  • asp.net
  • c#
  • html
  • javascript
  • model-view-controller

List: 如何为某个索引元素应用“where”条件? go 91
匿名

发表评论

匿名网友

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

确定

  • 开发者交流平台

    本页二维码