str*_*min 6 javascript jquery function onclick
我需要一种方法来将元素按下到该元素调用的函数中,然后禁用它。
我尝试使用 $(this),但不起作用,这是我的代码:
<script>
function x(){
$(this).prop("disabled",true);
}
<script>
<input type='button' onclick='x()' value='Disable me' />
<input type='button' onclick='x()' value='Disable me' />
Run Code Online (Sandbox Code Playgroud)
<script>
function x(){
$(this).prop("disabled",true);
}
<script>
<input type='button' onclick='x()' value='Disable me' />
<input type='button' onclick='x()' value='Disable me' />
Run Code Online (Sandbox Code Playgroud)
function x(){
$(this).prop("disabled",true);
}Run Code Online (Sandbox Code Playgroud)
我想避免添加和调用 id,有什么办法可以做到吗?
谢谢你!
- 编辑 -
解决方案1:
您可以简单地附加一个单击事件处理程序,该处理程序以按钮类型的输入为目标,然后用于this定位调用事件的元素。
<script>
$(document).ready(function(){
$("input:button").click(function()
{
$(this).prop("disabled",true);
});
});
</script>
<input type='button' value='Disable me' />
<input type='button' value='Disable me' />
Run Code Online (Sandbox Code Playgroud)
示例:https: //jsfiddle.net/DinoMyte/jmLynLsg/1/
解决方案2:
您可以从 javascript 函数传递this关键字,该函数将充当指向元素的指针。
<script>
function x(obj){
$(obj).prop("disabled",true);
}
</script>
<input type='button' onclick='x(this)' value='Disable me' />
<input type='button' onclick='x(this)' value='Disable me' />
Run Code Online (Sandbox Code Playgroud)
示例:https: //jsfiddle.net/DinoMyte/jmLynLsg/2/
| 归档时间: |
|
| 查看次数: |
16214 次 |
| 最近记录: |