[英]Need help understanding jQuery functions
I've spent a few days going over the guides on the jQuery site in an attempt to learn it. 我花了几天时间浏览jQuery网站上的指南,以尝试学习它。 I have a pretty good grasp on it, and javascript.
我对它和javascript有很好的了解。 However, I'm trying to put it to use and I'm a bit confused.
但是,我试图使用它,我有点困惑。
here's the situation: I want to have a function that accepts parameters, and when called, will use those parameters to set the inner html of a div. 情况是这样:我想有一个接受参数的函数,当调用该函数时,将使用这些参数来设置div的内部html。
In regular JS I would do something like: 在常规JS中,我将执行以下操作:
function showMessage(type, title, message){
div.innerHTML = "hello world!";
}
It would obviously use the parameters but for the sake of simplicity I didn't. 它显然会使用参数,但是为了简单起见,我没有使用。
I know in jQuery, to do the same thing you would do: 我知道在jQuery中,您可以执行相同的操作:
$('#id').html('Hello world!');
However, to do that I'd need it in a document ready function. 但是,要做到这一点,我需要在文档就绪功能中使用它。 I've also experimented with
我也尝试过
$('#close').click(function( event ) {
do stuff;
}
With the original JS function, I could simply do an 使用原始的JS函数,我可以简单地执行
onClick="showMessage"
Is there a way to call functions like that in jQuery? 有没有办法在jQuery中调用类似的函数? or do I need to use .click listeners?
还是我需要使用.click监听器? I don't know a terrible lot about jQuery, and I don't know everything that my system will need to be able to do in the future, so I'd rather have a way to call the function when I need it.
我对jQuery并不了解很多,也不知道我的系统将来需要做的所有事情,所以我宁愿有一种在需要时调用该函数的方法。 Also, how do I pass parameters to the jQuery function?
另外,如何将参数传递给jQuery函数?
Use .click() or .on() listeners in order to call your functions for the sake of keeping javascript calls out of your html 使用.click()或.on()侦听器来调用您的函数,以便将javascript调用排除在html之外
Also, how do I pass parameters to the jQuery function?
另外,如何将参数传递给jQuery函数?
you pass them into the function using an anonymous callback on your click event 您可以在点击事件中使用匿名回调将它们传递给函数
function showMessage(param1, param2) {
//do stuff with your params
}
jQuery('#id').click(function() {
showMessage(param1, param2);
});
The concept on creating function when using document ready: 使用文档就绪时创建函数的概念:
$(document).ready(function(){
//call function
showMessage("alert", "hello", "message")
})
function showMessage(type, title, message){
$('#id').html('type is :' +type+ ' title is :' +title+ 'message is'+ message);
}
Is this what you're looking for? 这是您要找的东西吗?
$(document).ready(function () {
$('#close').click(function() {
showMessage('id', 'Hello world!');
}
});
function showMessage(id, message){
$('#' + id).html(message);
}
using a $(document).ready()
function triggers the page and elements to listen to the defined events to occur and run functions accordingly. 使用
$(document).ready()
函数将触发页面和元素以侦听发生的已定义事件并相应地运行函数。 yet you can use JQuery facilities together with JavaScript style of scriptings. 但是,您可以将JQuery功能与JavaScript样式的脚本一起使用。
following might help: 以下内容可能会有所帮助:
<input type=text onchange="getName(this.value,'link.php');" />
JS: JS:
function getName(val, href){
var link = href +'?name='+val;
$('#result').load(link); // JQuery function
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.