Javascript 我能';不要改变功能范围
我有一个大型机和一个iframe:Javascript 我能';不要改变功能范围,javascript,jquery,scope,Javascript,Jquery,Scope,我有一个大型机和一个iframe: 我在iframe之前向大型机传递一个函数 大型机将beforeIframe的URL更改为changedIFrame 加载changedIFrame后,执行匿名函数 下面的代码是我想做的: mainFrame.jsp 我尝试过很多事情,但都做不到 主机 在您的示例中,包含在“beforeIframe.jsp”的randerLeftMenu调用中的$将始终限定在调用它的上下文中。只有此变量可以动态绑定到不同的范围。不过,您应该能够通过将函数作为字符串传递(或依赖f
在您的示例中,包含在“beforeIframe.jsp”的
randerLeftMenu
调用中的$
将始终限定在调用它的上下文中。只有此
变量可以动态绑定到不同的范围。不过,您应该能够通过将函数作为字符串传递(或依赖function
的toString()
方法)并动态计算字符串来绕过此问题
已更新以包含工作示例(已测试的Firefox和IE):
函数randerLeftMenu(回调){
$(“#ifrm”).attr(“src”,“changedIframe”);
如果(回调){
$(“#ifrm”).load(函数(e){
试一试{
//此脚本注入和设置超时仅是必需的
//如果不想将iframe源更改为添加jQuery;
//否则,您只需要eval()。
var script=document.createElement('script');
script.src=”http://code.jquery.com/jquery-latest.js";
e、 target.contentWindow.document.body.appendChild(脚本);
setTimeout(函数(){
//var evalStr='e.target.contentWindow.eval(\'('+callBack.toString().replace(//\n/g',)+')()\');//您可以传递函数并将其转换为Firefox的字符串,但IE当然不支持它,因此您需要将函数作为字符串传递
var evalStr='e.target.contentWindow.eval(\'('+callBack.replace(/[\r\n]/g',)+')()\';
eval(evalStr);
}, 500);
}
捕获(e){
警报(e);
}
});
}
}
我已经多次阅读您的问题,不知道您在寻求什么帮助。我没有多次阅读您的问题,但不清楚您想要实现什么目标。你将不得不重新表述其中的大部分内容。等待它反映出我的变化,然后再这样做。你想实现什么?
function randerLeftMenu(callBack) {
$("#ifrm").attr("src", "<ui:context />/changedIframe")
if(callBack) {
$("#ifrm").load(function() { callBack(); });
}
}
<input type="text" value="testVal" id="docType" class="input width2">
function randerLeftMenu(callBack) {
if(callBack) {
$("#ifrm").load(function() {
with(this.contentWindow) { callBack(); }
callBack.apply(this);
eval(callBack());
callBack.call(this);
$.proxy(callBack(),this.contentWindow);
});
}
}
function randerLeftMenu(callBack) {
$("#ifrm").attr("src", "<ui:context />/changedIframe");
if(callBack) {
$("#ifrm").load(function(e) {
try {
// This script injection and setTimeout is only necessary
// if you don't want to change the iframe source to add jQuery;
// otherwise, you only need the eval().
var script = document.createElement('script');
script.src = "http://code.jquery.com/jquery-latest.js";
e.target.contentWindow.document.body.appendChild(script);
setTimeout(function () {
// var evalStr = 'e.target.contentWindow.eval(\'('+callBack.toString().replace(/\n/g, ' ')+')()\')'; // You can pass the function and convert to a string for Firefox, but IE of course doesn't support it, so you will need to pass the function as a string
var evalStr = 'e.target.contentWindow.eval(\'('+callBack.replace(/[\r\n]/g, ' ')+')()\')';
eval(evalStr);
}, 500);
}
catch(e) {
alert(e);
}
});
}
}