单击jquery调用函数
我在单击时调用函数时遇到问题。以下是我的代码: JavaScript:单击jquery调用函数,jquery,Jquery,我在单击时调用函数时遇到问题。以下是我的代码: JavaScript: $(document).ready(function(){ function showMessage(msg) { alert(msg); }; }) <input type="button" value="ahaha" onclick="$(this).showMessage('msg');" /> HTML: $(document).ready(
$(document).ready(function(){
function showMessage(msg) {
alert(msg);
};
})
<input type="button" value="ahaha" onclick="$(this).showMessage('msg');" />
HTML:
$(document).ready(function(){
function showMessage(msg) {
alert(msg);
};
})
<input type="button" value="ahaha" onclick="$(this).showMessage('msg');" />
由于某些原因,我无法使用click()
函数。请帮帮我。没有$(这个)
请。。只需这样打电话:onclick=“showmessage('msg')”
无需$(此)
请。。只需这样调用:onclick=“showmessage('msg')”
一些事情:
不是jQuery对象的属性。这只是一个常规函数showMessage()
- 如果您使用的是jQuery,请不要使用
。用JavaScript绑定所有事件onclick
$.fn.showMessage = function(message) {
alert(message);
};
$(document).ready(function() {
$('#button').on('click', function() {
$(this).showMessage('I am a message');
});
});
<input type="button" value="ahaha" id="button" />
HTML:
$.fn.showMessage = function(message) {
alert(message);
};
$(document).ready(function() {
$('#button').on('click', function() {
$(this).showMessage('I am a message');
});
});
<input type="button" value="ahaha" id="button" />
有几件事:
不是jQuery对象的属性。这只是一个常规函数showMessage()
- 如果您使用的是jQuery,请不要使用
。用JavaScript绑定所有事件onclick
$.fn.showMessage = function(message) {
alert(message);
};
$(document).ready(function() {
$('#button').on('click', function() {
$(this).showMessage('I am a message');
});
});
<input type="button" value="ahaha" id="button" />
HTML:
$.fn.showMessage = function(message) {
alert(message);
};
$(document).ready(function() {
$('#button').on('click', function() {
$(this).showMessage('I am a message');
});
});
<input type="button" value="ahaha" id="button" />
$(文档).ready(函数(){
函数showMessage(msg){
警报(msg);
};$(文档).ready(函数(){
函数showMessage(msg){
警报(msg);
}有几种方法可以做到这一点
让我对您的html进行一些更改
<input id="myTest" value="CLICK ME!!!" title="HELLO WORLD!!!" type="button" />
希望这有帮助,
干杯有几种方法可以做到这一点
让我对您的html进行一些更改
<input id="myTest" value="CLICK ME!!!" title="HELLO WORLD!!!" type="button" />
希望这有帮助,
干杯额外的分号没有什么区别。注意缺少的分号。额外的分号没有什么区别。注意缺少的分号。从所有可能的解决方案来看,扩展jQuery对象只会使函数调用更复杂,并增加创建不必要的jQuery对象的开销t如果showMessage()
修改当前对象(因此是$(this)
)来显示消息?在绑定侦听器时,我将+1,OP可以决定除此之外保留什么是重要的。我喜欢第一点。切中要害的是,解决方案非常干净,而且是oo方式:)+1从所有可能的解决方案来看,扩展jQuery对象只会使函数调用更加复杂,并增加创建不必要的jQuery对象的开销。showMessage()
修改当前对象(因此$(this)
)会怎么样若要显示消息,请在绑定侦听器时使用+1,OP可能会决定除此之外还需要保留哪些内容。我喜欢第一点。切中要害。此外,解决方案非常简洁且面向对象:)+1我在打电话,我找不到强调代码的符号:D这里,修复了代码。但我认为函数定义仍然存在闭包问题。我在打电话,我找不到强调代码的符号:D这里,修复了代码。但我认为函数定义仍然存在闭包问题。。