jQuery - 调用 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15248035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:02:55  来源:igfitidea点击:

jQuery - calling a javascript function

javascriptjqueryfunctioncall

提问by frrlod

function initiate()
{

    $("p").text("Hi", Hello());
}

function Hello()
{
    alert("Hello, you have called another function");
}

HTML:

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".

现在脚本在单击按钮时调用函数initial(),然后调用另一个函数Hello(),同时将html 中的所有段落更改为“Hi”。

What I would like is that the function Initiate() will call the function Hello() using jQuery, and do nothing else.

我想要的是函数 Initiate() 将使用 jQuery 调用函数 Hello(),并且不执行任何其他操作。

(In other words, how do I simply call a function using jQuery?)

(换句话说,我如何简单地使用 jQuery 调用函数?)

采纳答案by StormBeast

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:

我会说你真的不需要jquery,但是如果你死心塌地使用它,你应该在这里重新评估你的逻辑。先给你的按钮一个 id,你不需要它,但无论如何这样做是好的:

<button id="btnClickMe">Click Me<button>

then, use that to trigger a jquery event on, thus calling your function:

然后,使用它来触发 jquery 事件,从而调用您的函数:

$('#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
}); 

回答by Adil

You could not pass callback function to text(). Call the hellomethod after changing the text.

您无法将回调函数传递给text()hello更改文本后调用该方法。

Live Demo

现场演示

Change

改变

$("p").text("Hi", Hello());

To

$("p").text("Hi");
Hello();

回答by Evan Knowles

If you only want to call Hello() from Initiate(), you simply call it as follows:

如果您只想从 Initiate() 调用 Hello(),您只需按如下方式调用它:

function initiate()
{

    Hello();
}

回答by Esailija

What I would like is that the function Initiate() will call the function Hello() using jQuery, and do nothing else.

我想要的是函数 Initiate() 将使用 jQuery 调用函数 Hello(),并且不执行任何其他操作。

You don't need to call the function using jQuery, you can just call it.

您不需要使用 jQuery 调用该函数,您只需调用它即可。

function initiate()
{

    $("p").text("Hi");
    Hello();
}

The function argument taken by .textis 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").

所采用的函数参数.text不是第二个参数,也不是像您尝试那样调用该函数。它用于通过从传递的函数返回一个值来单独设置每个段落的文本。由于您希望每个段落中都有文本“Hi”,因此您可以使用$("p").text("Hi").

回答by Kevin Bowersox

This will attach an event handler that is fired when the button is clicked. You will not need the initializefunction 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.

这将附加一个在单击按钮时触发的事件处理程序。您将不再需要该initialize功能,也不需要在p. 我无法辨别您想要什么,但是我认为您可能只想通过单击按钮来调用该函数。

$(document).ready(function(){
  $("button").click(function(){
     Hello();
  });
});