admin管理员组文章数量:1290949
function initiate()
{
$("p").text("Hi", Hello());
}
function Hello()
{
alert("Hello, you have called another function");
}
HTML:
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button onclick="initiate()">Click me</button>
Right now the script calls the function initiate() upon the button click, and then the other function, Hello() is called, while changing all paragraphs in the html to "Hi".
What I would like is that the function Initiate() will call the function Hello() using jQuery, and do nothing else.
(In other words, how do I simply call a function using jQuery?)
function initiate()
{
$("p").text("Hi", Hello());
}
function Hello()
{
alert("Hello, you have called another function");
}
HTML:
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button onclick="initiate()">Click me</button>
Right now the script calls the function initiate() upon the button click, and then the other function, Hello() is called, while changing all paragraphs in the html to "Hi".
What I would like is that the function Initiate() will call the function Hello() using jQuery, and do nothing else.
(In other words, how do I simply call a function using jQuery?)
Share Improve this question asked Mar 6, 2013 at 13:00 frrlodfrrlod 6,6858 gold badges33 silver badges38 bronze badges 1- 1 Well, how do you call a function in JavaScript? – Ja͢ck Commented Mar 6, 2013 at 13:53
5 Answers
Reset to default 3You could not pass callback function to text(). Call the hello
method after changing the text.
Live Demo
Change
$("p").text("Hi", Hello());
To
$("p").text("Hi");
Hello();
I would say you don't really need jquery for this, but if you are dead set on using it, you should re-evaluate your logic here. Give your button an id first, you don't need it, but it's good to do it anyway:
<button id="btnClickMe">Click Me<button>
then, use that to trigger a jquery event on, thus calling your function:
$('#btnClickMe').click(function() { //You can either link directly to a func here
alert('Whatever'); //Or you can write the code for your func directly into the handler here
});
If you only want to call Hello() from Initiate(), you simply call it as follows:
function initiate()
{
Hello();
}
What I would like is that the function Initiate() will call the function Hello() using jQuery, and do nothing else.
You don't need to call the function using jQuery, you can just call it.
function initiate()
{
$("p").text("Hi");
Hello();
}
The function argument taken by .text
is not the second argument and neither is it for calling that function like you are trying to do. It is for setting the text of each paragraph individually by returning a value from the passed function. Since you want the text "Hi" in each paragraph, you can just use $("p").text("Hi")
.
This will attach an event handler that is fired when the button is clicked. You will not need the initialize
function anymore nor will you need to set the text on the p
. I'm having trouble discerning what you would like, however I think you may just want to call the function on a simple click of the button.
$(document).ready(function(){
$("button").click(function(){
Hello();
});
});
本文标签: jQuerycalling a javascript functionStack Overflow
版权声明:本文标题:jQuery - calling a javascript function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741511429a2382632.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论