var index = 0;
默认情况下,索引值为0,现在我有两个按钮,当我单击按钮1或2时,索引值应该增加。第二个按钮单击后应该从1开始取值... 同样的,第三个按钮以此类推。
var index = 0;
默认情况下,索引值为0,现在我有两个按钮,当我单击按钮1或2时,索引值应该增加。第二个按钮单击后应该从1开始取值... 同样的,第三个按钮以此类推。
您的问题不够清晰,但我认为您正在寻找类似于这样的东西...
<script type="text/javascript">
index = 0;
increment = function(){ index++; }
</script>
<button name="button1" onclick="increment();">Button 1</button>
<button name="button2" onclick="increment();">Button 2</button>
function incrementIndex() {
index += 1;
}
index
变量是全局的。click
事件。在JavaScript中,有三种方法可以实现这一点。
Inline JavaScript
<button onclick="incrementIndex()">Button 1</button>
<button onclick="incrementIndex()">Button 2</button>
This is how Hitler developed websites. Don't do this.
Event Handler
This requires you to wait until the DOM has loaded, and then "get" your two buttons somehow. If you only have two buttons on the page, you can do something like this (but only if this script comes after the HTML in your document):
var buttons = document.getElementsByTagName('button');
buttons[0].onclick = buttons[1].onclick = incrementIndex;
Note in that example that there are no parentheses ()
at the end of incrementIndex
.
Event Listener
The problem with event handlers is that you can only attach one function at a time. If you're going to have other JavaScript listening for button clicks, you'll have to use event listeners.
But event listeners are a major pain in the butt because IE6-8 does it wrong, and (depending on the project) you'll probably have to code around it. At it's simplest, here's how to add an event listener in most browsers:
var addEvent = window.addEventListener ? function (elem, type, method) {
elem.addEventListener(type, method, false);
} : function (elem, type, method) {
elem.attachEvent('on' + type, method);
};
addEvent(buttons[0], 'click', incrementIndex);
addEvent(buttons[1], 'click', incrementIndex);
That is just the surface of cross-browser event listening. If you want to learn more, QuirksMode has a good series of articles on it.
<html>
<head>
<script type="text/javascript">
var index = 0;
</script>
</head>
<body>
<button id="button1" onclick="index=index + 1;">1</button>
<button id="button2" onclick="index=index + 1;">2</button>
</body>
</html>
<html>
<head>
<script type="text/javascript">
var index = 0;
</script>
</head>
<body>
In response to <a href="https://dev59.com/k1bTa4cB1Zd3GeqP-mwn">theJava's question at StackOverflow</a>
<button onclick="index = index + 1;document.getElementById('tb').value = index;">Button1</button>
<button onclick="index = index + 1;document.getElementById('tb').value=index;">Button2</button>
<input type="text" id="tb"/>
</body>
</html>
Demo:
<script type="text/javascript">
var index = 0;
function increment(var1)
{
index++;
if(var1 == 1)
{ document.getElementById('button1').value=index; }
else
{ document.getElementById('button2').value=index; }
}
</script>
<input type="button" id="button1" onclick="increment(1)" value="increment me!"/>
<input type="button" id="button2" onclick="increment(2)" value="increment me!"/>
这可能是你正在寻找的内容。
https://codepen.io/anon/pen/oPLVdE
document.getElementById('btn').addEventListener('click', function(){
var i = document.getElementById("like").innerHTML;
i++;
document.getElementById("like").innerHTML = i;
})
document.getElementById('btn2').addEventListener('click', function(){
var i = document.getElementById("like").innerHTML;
i++
document.getElementById("like").innerHTML = i;
})