我想让多个按钮使用相同的功能。这就是我想要做的事情。
<button><a herf="#" onClick="myFunction('clk1',c1);">button</a></button>
<p id="clk1"></p>
使用按钮我想更改变量并更改段落
<script>
var c1 = 0;
function myFunction(paragraph,varibl) {
varibl += 1;
document.getElementById(paragraph).innerHTML = varibl;
}
</script>
我环顾四周,找不到任何东西。这不起作用,我不知道如何使它工作。
完整代码:
<!DOCTYPE html>
<html>
<meta charset="utf-8"/>
<head>
<title>clicker clicker</title>
<style>
html {
text-align: center;
background-color: #d1d1d1;
font-family:Verdana;
}
.onebutton {
background-color: #f4f4f4;
border: 2px solid #5b5b5b;
color: #5b5b5b;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 32px;
cursor: pointer;
border-radius: 8px;
box-shadow:inset 0px 12px 22px 2px #ffffff;
}
.onebutton:active {
background-color: #e5e5e5;
}
.twobutton {
background-color: #f4f4f4;
border: 2px solid #5b5b5b;
color: #5b5b5b;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
border-radius: 8px;
box-shadow:inset 0px 12px 22px 2px #ffffff;
}
.twobutton:active {
background-color: #e5e5e5;
}
</style>
</head>
<body>
<h1>Clicker Clicker</h1>
<p id="number"></p>
<button class="onebutton"><a herf="#" onClick="clc();">click</a>
</button>
<br>
<p>clicker:100</p>
<button class="twobutton"><a herf="#"
onClick="upgrade('clk1',c1);">buy</a></button>
<p id="clk1"></p>
<p>clicker:1000</p>
<button class="twobutton"><a herf="#" onClick="upgrade('clk2',c2);">buy</a></button>
<p id="clk2"></p>
<script>
var number = 0;
var c1 = 0;
var c2 = 0;
document.getElementById("number").innerHTML = number;
function clc() {
number += 1;
document.getElementById("number").innerHTML = number;
}
function update() {
number += c1;
document.getElementById("number").innerHTML = number;
c1 += c2;
document.getElementById("clk1").innerHTML = c1;
}
function upgrade(what,clicker) {
window[clicker] += 1;
document.getElementById(what).innerHTML = clicker;
}
setInterval(update, 100);
</script>
</body>
</html>
这是在这里所以它并没有说我需要很多代码sdljnvaksjdnfblkajsdbfjmas dbfmha bsdmnfb admsf bds msadf
答案 0 :(得分:1)
这样做:
<button><a herf="#" onClick="myFunction('clk1');">button</a></button>
<p id="clk1"></p>
然后:
<script>
var c1 = 0;
function myFunction(paragraph) {
c1++;
document.getElementById(paragraph).innerHTML = c1;
}
</script>
每次调用此方法时,变量都会增加一个! jsfiddle
答案 1 :(得分:1)
如果你必须使用签名传递变量,并且不能简单地(根据其他答案)直接引用函数中的正确变量,这里是你做的。
JS 始终按值传递变量,而不是引用。但是,如果您发送一个对象,那么&#34;值&#34;实际上是对原始对象的引用。所以你可以这样做:
var counters = { a: 0 };
function test(key) {
counters[key]++;
console.log(counters);
}
&#13;
<button onclick="test('a')">Click</button>
&#13;
答案 2 :(得分:1)
用户可以使用对象轻松修改函数内的值
git log --author=USERNAME --oneline | awk '{print $1}' | xargs git show
和脚本
public static void scrollToView(final ScrollView scrollView, final View view) {
view.requestFocus();
final Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (!view.getLocalVisibleRect(scrollBounds)) {
new Handler().post(new Runnable() {
@Override
public void run() {
scrollView.smoothScrollTo(0, view.getBottom());
}
});
}
}
Button clickButton = (Button) findViewById(R.id.yout_button_id);
clickButton.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
String n = ((EditText) findViewById(R.id.your_edittext_id)).getText();
int tn = Integer.parseInt(n); //TODO: check the view exists
TextView tv = (TextView)((LinearLayout)findViewById(R.id.your_linearlayout_id)).getChildAt(tn);
scrollToView((ScrollView)findViewById(R.id.yout_scrollview_id, tv);
}
});
答案 3 :(得分:0)
<body>
<button><a herf="#" onClick="myFunction('clk1','c1');">Para 1</a></button>
<p id="clk1"></p>
<button><a herf="#" onClick="myFunction('clk2','c2');">Para 2</a></button>
<p id="clk2"></p>
<button><a herf="#" onClick="myFunction('clk3','c3');">Para 3</a></button>
<p id="clk3"></p>
<script>
var c = {c1:0, c2:0, c3:0};
function myFunction(paragraph,varibl) {
document.getElementById(paragraph).innerHTML = ++c[varibl];
}
</script>
</body>
&#13;