[英]declared variable doesn't seem global
I declared my variable first - then have my jquery : 我首先声明了变量-然后有了我的jquery:
var cimg = '';
jQuery(function($) {
$(".trigger").click(function() {
var cimg = event.target.id;
clickedimage();
loadPopup();
});
});
Then the script outside the jquery : 然后在jquery之外的脚本:
function clickedimage()
{
var xmargin = $(cimg).width()/2;
var ymargin = $(cimg).height()/2;
$("#popupimage").css('margin-left',-xmargin+'px')
$("#popupimage").css('margin-top',-ymargin+'px')
var Clicked = cimg.src;
$('#popupimage').attr('src',Clicked);
}
It doesn't seem to want to take on the cimg value supplied from the .trigger
clicked event 它似乎不希望采用
.trigger
clicked事件提供的cimg值
What am I doing wrong here? 我在这里做错了什么?
In the click event you do var cimg = ...;
在click事件中,您可以执行
var cimg = ...;
. 。 The
var
keyword tells it to create a new local variable instead of using the one from the outer scope. var
关键字告诉它创建一个新的局部变量,而不是使用外部范围的变量。
Remove the var
inside the handler. 删除处理程序内部的
var
。
var cimg = '';
jQuery(function($) {
$(".trigger").click(function(event) {
cimg = event.target.id;
clickedimage();
loadPopup();
});
});
You redeclaring var cimg
which causes it to be a local variable inside function scope. 您重新声明了
var cimg
,这使它成为函数范围内的局部变量。 In addition, you're not providing event
, which also causes an error (some browsers allow referring to window.event though). 另外,您没有提供
event
,这也会导致错误(尽管某些浏览器允许引用window.event)。
jQuery(function($) {
$(".trigger").click(function(event) {
cimg = event.target.id;
clickedimage();
loadPopup();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.