Why does my jQuery click handler appear to run mul

2019-02-16 10:05发布

I apply:

$(".newContentLink").click(function() {
    $("#test").append("1");
});

On this:

<span id="contents">
<input class="newContentLink" type="submit" style="width: 100%;" value="CREATE A NEW CONTENT"/>
<span id="content1" class="content study">
</span>
<input class="newContentLink" type="submit" style="width: 100%;" value="CREATE A NEW CONTENT"/>
<span id="content3" class="content study">
</span>
<input class="newContentLink" type="submit" style="width: 100%;" value="CREATE A NEW CONTENT"/>
<span id="content4" class="content category">
</span>
<input class="newContentLink" type="submit" style="width: 100%;" value="CREATE A NEW CONTENT"/>
</span>

How come when I click on the first 2 buttons it adds 111, the next button adds 11, and the last one adds 1?

7条回答
Viruses.
2楼-- · 2019-02-16 10:29

Not a jQuery bug - Working Demo

There must be a problem with something else in your code. Where is the element with id="test" in your markup?

EDIT:

Just read chaos' answer, which sounds like a plausible explanation.

By the way, element ids must be unique within HTML markup - this is part of the HTML specification and may be another possible explanation

查看更多
登录 后发表回答