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

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

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

问题

<button class="anunt-edit" type="submit" data-id="@anunt.Id" id="editButton" >
    @Html.Partial("editSVGPartial", new {@Id="editButton", @class="svg"})
</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 )
document.addEventListener("click", e => 
    {
        var isEditButtonClicked = e.target.matches("#editButton");
        if ( isEditButtonClicked )
        {
            console.log("button clicked")
        }
        else
        {
            console.log("click")
        }
    })
英文:
                                <button class="anunt-edit" type="submit" data-id="@anunt.Id" id="editButton" >
                                    @Html.Partial("editSVGPartial", new {@Id="editButton", @class="svg"})
                                </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 )

document.addEventListener("click", e => 
    {
        var isEditButtonClicked = e.target.matches("#editButton");
        if ( isEditButtonClicked )
        {
            console.log("button clicked")
        }
        else
        {
            console.log("click")
        }
    })

答案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
匿名

发表评论

匿名网友

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

确定