简体   繁体   English

如何将参数传递给<span>标签</span>上的 jQuery.click() 函数<span>?</span>

[英]How do I pass parameters into a jQuery.click() function on a <span> tag?

I have used the jQuery.click() function in the past with buttons and have had nothing but success, but I tried to use it to click on a <span> and it doesn't work.我过去曾使用 jQuery.click() 函数与按钮一起使用,但除了成功之外一无所有,但我尝试使用它来单击<span>并且它不起作用。

Also I noticed that it automatically executed my function without listening to the click.我还注意到它会自动执行我的功能而不听点击。

Judging how I used it on buttons, this would be the syntax:判断我如何在按钮上使用它,这将是语法:

<p><span id="example">Click Here</span></p>

   $("#example").click(exampleFunction(p1, p2));

But it does not seem to work.但它似乎不起作用。 Again it just executes it without the click even taking place.同样,它只是执行它,甚至没有点击。 I even tried:我什至试过:

$(document).on("click", "#example", exampleFunction(p1, p2));

Still no luck, same results.仍然没有运气,同样的结果。

I am making a weather app and my goal with this is to toggle the temperature between Fahrenheit and Celsius by clicking on the unit.我正在制作一个天气应用程序,我的目标是通过单击单位在华氏度和摄氏度之间切换温度。 I made a copy of the code for the app on codepen.io here:我在 codepen.io 上复制了该应用程序的代码:

Codepen Weather App Codepen 天气应用

I appreciate the help!我感谢您的帮助!

Looks like you should try something like this:看起来你应该尝试这样的事情:

$(document).on("click", "#example", function() {
  console.log('Hello World');
});

Using the function() definition alone give me Uncaught SyntaxError: Unexpected token ( .单独使用function()定义会给我Uncaught SyntaxError: Unexpected token ( .

What you're doing is binding an anonymous function to the click.您正在做的是将匿名函数绑定到点击。 If you were doing this somewhat differently, like MyFunction() , then it would only execute the function.如果您这样做有点不同,比如MyFunction() ,那么它只会执行该函数。

If you had MyFunction you could still trigger it using click like so:如果你有MyFunction你仍然可以像这样使用 click 来触发它:

function MyFunction() {
  console.log('Hurra!')
}

$('#example').click(MyFunction)

Notice that I didn't use the parentheses otherwise it will actually run the function instead of binding it.请注意,我没有使用括号,否则它将实际运行该函数而不是绑定它。

Try it like this:像这样尝试:

 $('#example').click(bleepme); function bleepme(){ alert('hi'); }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <p><span id="example">Click Here</span></p>

Note that the on-click call to the bleepme function does not have parens -- if you put parens there, it will run the function at document.ready, but not upon click.请注意,对 bleepme 函数的单击调用没有括号——如果您将括号放在那里,它将在 document.ready 处运行该函数,但不会在单击时运行。

Example - no parens on click示例 - 单击时没有括号

Example 2 - parens on click示例 2 - 点击括号

Well you should supply a body to the function :)那么你应该为函数提供一个主体:)

$("#example").click(function() {
    alert('clicked');
});

I don't think that you really want to pass parameters into into the function.我不认为您真的想将参数传递给函数。 By the time someone is clicking on the temperature to toggle the units, the document.ready function has finished executing.当有人点击温度来切换单位时,document.ready 函数已经执行完毕。 You actually want to read the values for temp and units from the DOM or from local storage.您实际上想从 DOM 或本地存储中读取 temp 和 units 的值。 So, you don't need to pass parameters into your function, which should make things a bit easier.因此,您不需要将参数传递到您的函数中,这应该会使事情变得更容易一些。

I think you code need to work like this way我认为你的代码需要像这样工作

$(document).ready(function(){
  $('#example').on('click',function(){
    // Some of your logic here
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM