-3

我需要将 jQuery 对象传递给 eval 的解决方法。问题是我需要访问一个位于 eval 区域之外的 jQuery 对象,但我看不到将其传递进来。这就是我所拥有的。

var  jObj = $(selector);
var myCode = "var jObj="+jObj+"; var i="+i+"; "+shape.mouseover.onEnd.replace("\u0027","'");
var myFucn = new Function(myCode);
myFucn();

我要从中取出字符串的对象是

shape.mouseover.onEnd.replace("\u0027","'");

正在工作,我在该字符串中传递的是

open_info(jObj,i)

这是我必须开火的。交易是代码是通过 YUI 压缩器运行的,所以 jObj var 变成了别的东西,所以我需要把它传递进去。现在我在它认为应该有的地方遇到一个错误,并且结束 ] 这是不正确的。我似乎在工作,而不是 jObj var。

编辑

有很多方法可以到达我需要去的地方,但并不像 如何以对象形式在 eval 中传递参数?

shape.mouseover.onEnd = "open_info(jObj,i)";
/*
 * this is coming in and must be as it is, don't say it's wrong please
 * it's not able to be done anyother way!
 */


//lets process the string and pull in the vars
/* BEOFRE YUI COMPRESSOR CHANGES THINGS and works!!!
    var  jObj = $(selector);
    var i = 1;
    var myCode = shape.style.events.mouseover.onEnd.replace("\u0027","'");
    var myFucn = new Function(myCode); 
    myFucn();
 */
// AFTER note it can be random as i change code so it fails cause 
// var jObj is now var r and var i is now var e
    var  r = $(selector);
    var e = 1;
    var p= shape.style.events.mouseover.onEnd.replace("\u0027","'");
    var f=  new Function(p);
    f();

现在它在压缩之前工作..之后不是由于更改。希望能解决一些问题

4

2 回答 2

0

我可能走错了路,在这里感到困惑..

但这不是你想要做的吗?

向 myFucn 发送正确的对象以及我是什么

myFucn($(selector),10);

function myFucn(jObj,i)
{
shape.mouseover.onEnd.replace("\u0027","'");
}
于 2012-08-21T21:22:04.187 回答
0

我仍然不明白为什么这个问题得到了 2 票反对,但它已经解决并且效果很好。诀窍是对 dom 状态进行相同的操作。一旦它被放置,它真的很简单。

//so this is what the object is parsed out to from the json string
//since you can't just pass a function stright that way any how
shape.mouseover.onEnd = "open_info(jObj,i)"; 

//this is what will take that string and process it
//note jObj is what is in the orgain code but it changes to 
// var r or something else that is shorter after going thru YUI compressor
// Which is why we can't just use open_info(jObj,i) and it work.. 
// ie: it's not an issue with scoope but an issues with var names being shortened
(function(){
    //this is the trick on passing them so YUI doesn't get them
    //use a string and YUI skips it so we directly create the
    //needed oject in the window namespace 
    window['jObj']=jObj; window['i']=i;
    var p= shape.mouseover.onEnd;
    var f=  new Function(p); 
    f();
})();

就是这样.. 我把它放在点击或悬停事件中,所以它类似于 onClick。

/* EXMAPLE OUTPUT AFTER YUI COMPRESSION
//Note after the YUI compressor get ahold of that 
//processing code above it'll look like 
*/
function(){window.jObj=n,window.i=t;var u=i.mouseover.onEnd,r=new Function(u);r()}();

所以工作的方式是,我需要解决 var jObj 被重命名的问题。所以我只是简单地为名称做了一个刺痛,然后让压缩的 var 名称填充我需要处理的代码字符串的对象的名称。不知道为什么我以前没有看到它,我会保存我的代表值:-\ .. 哦,好吧。可能是缩短它的一种方法,但我现在要离开它。

编辑

我放弃了它正在工作的编辑。:) 很好.. 想知道还有什么其他方法可以让它做同样的事情。

于 2012-08-22T15:01:34.163 回答