Javascript + HTML with Arguments
Javascript + HTML with Arguments
我有一个有 2 行的表格,每行有 2 列。我想在点击 td 时更改 td 的内部 HTML。那是我
<table border="1">
<tr id="r1">
<td id="b1_1" onclick="b(b1_1)">0</td>
<td id="b1_2" onclick="b(b1_2)">0</td>
</tr>
<tr id="r2">
<td id="b2_1" onclick="b(b2_1)">0</td>
<td id="b2_2" onclick="b(b2_2)">0</td>
</tr>
</table>
function _(x){
return document.getElementById(x);
}
function b(bid){
_x(bid).innerHTML = "5";
}
b1_1
是一个标识符(对于未定义的变量)。你想要一个字符串文本。 'b1_1'
所有其他论点也是如此。
此外,您正在尝试调用名为 _x
的函数,但已定义一个名为 _
的函数。
不过,使用单个事件侦听器而不使用 id 会更整洁。
function b(event){
event.target.innerHTML = 5;
}
document.querySelector('table').addEventListener('click', b);
您的函数被错误调用 ( _x
) - 您已将其声明为简单的 _
。因此,您的b
函数应如下所示:
function b(bid){
_(bid).innerHTML = "5";
}
在onclick
处理程序中传递的参数也需要是字符串,因此括在引号中,即:onclick="b('b1_1')"
或者,您可以在这些onclick
处理程序中使用this
:
<td id="b1_1" onclick="b(this)">0</td>
你的函数将变成:
function b(bid){
bid.innerHTML = "5";
}
替代
删除HTML上的onclick
,并使用jQuery:
$('tr td').click(function(){
var previousVal = parseInt($(this).html());
$(this).html(previousVal + 5);
});
每次您点击div 时,它都会添加 5(我认为这可能是您想要的,因为您正在谈论出价)。
相关文章:
- Fighting with FRP
- issue with FB.Event.subscribe
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- WebComponentsJS with IE10
- 指令的模板必须只有一个根元素:With restrict E&替换true
- timeago.js with datatable and PHP
- Ajax and Json with Rails
- errors with Javascript try catch
- how to split a string with ','
- Understanding Javascript scope with "var that = this&qu
- Architecture for CPU intensive tasks with NodeJS & Socke
- How to declare a Map containing certain properties with flow
- 设置'这'在React with Inverse Data Flow中
- Unit-testing multiple use cases with Karma & Mocha.js
- Modify Javascript with C#
- Javascript + HTML with Arguments
- Javascript Q Promise Sequence Order with Arguments
- Javascript function arguments with "-"