jQuery - 调用函数内联 [英] jQuery - Calling a function inline

查看:80
本文介绍了jQuery - 调用函数内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个变量传递给内联的jQuery函数(即:在实际链接中使用 onMouseOver =function();(这是一个区域)来自图像映射的标签))。

I am trying to pass one variable to a jQuery function inline (ie: using an onMouseOver="function();" within the actual link (which is an area tag from an image map)).

只有在我将它放在 $(文件).ready(函数(函数)之前才调用该函数。 ){行,但这样做会导致jQuery出现各种问题。

The function is only being called if I place it before the $(document).ready(function(){ line, but doing this is causing all sorts of problems with jQuery.

我想要的只是一个简单的标签(例如< area shape =circlecoords =357,138,17onMouseOver =change('5');id =5/> 启动功能它包含在普通的jQuery代码体中。

All I want is for a simple tag (such as <area shape="circle" coords="357,138,17" onMouseOver="change('5');" id="5" /> to launch a function that is contained within the normal jQuery body of code.

非常感谢您提供的任何帮助。

Many thanks for any help you can offer.

为了说明这一点,以下工作:

To illustrate the point, the following works:

<script type="text/javascript">
function myfunction(x)    {  alert(x); //Alerts 2  
}
</script>

<img src="/shared_images/loading.gif" border="0" usemap="#Map">
<map name="Map"><area shape="rect" coords="171,115,516,227"
onMouseOver="myfunction('2')"></map>

但以下不是

<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
function myfunction(x)    {  alert(x); //Nothing happens   
}
}
</script>

<img src="/shared_images/loading.gif" border="0" usemap="#Map">
<map name="Map"><area shape="rect" coords="171,115,516,227"
onMouseOver="myfunction('2')"></map>


推荐答案

在你的第二个例子中,你已宣布 myfunction 你要传递给 .ready()的匿名函数。这意味着 myfunction 是一个本地的变量,它只在匿名函数的范围内,你不能从其他任何地方调用它。

In your second example, you've declared myfunction inside the anonymous function you're passing to .ready(). That means myfunction is a local variable, which is only in scope inside that anonymous function, and you cannot call it from anywhere else.

有两种解决方案。

首先,你可以在调用 .ready()之外(之前或之后)声明它。这个不应该因为任何干扰jQuery。如果确实如此,其他错误(可能是一个简单的语法错误?)我们欢迎您提出StackOverflow问题。

First, you can declare it outside (before or after) the call to .ready(). This should not cause any interference with jQuery. If it does, something else is wrong (perhaps a simple syntax error?) that we'd welcome you to bring up in a StackOverflow question.

其次,您不应该使用 onMouseOver =附加事件处理程序(因为它混合了JavaScript和HTML),所以让我们完全废除它。用以下内容替换你的JavaScript:

Second, you shouldn't be using onMouseOver="" to attach event handlers (as that mixes JavaScript with HTML), so let's do away with it entirely. Replace your JavaScript with this:

$(document).ready(function() {
    $("#that-area-down-there").mouseover(function() {
        alert(2);
    });
});

以及您的HTML:

<area shape="rect" coords="171,115,516,227" id="that-area-down-there" />

(想必你要替换 id 当然,在上下文中有更有意义的东西。)

(Presumably you'll want to replace that id with something more meaningful in context, of course.)

这篇关于jQuery - 调用函数内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆