高手看这标题会不会感觉无奈.. jquery本来就是javascript嘛。
下边是菜鸟的问题, js执行不到jquery里的动作a.html 是jquery
<javascript>
$(function()){ function hello(){
alert('123');
}
……省略好多function……
</javascript>b.html 是js
parent.window.hello();以上的代码,a.html用js的话是可以执行的, 但改用jquery就不行了, jquery 一开始的$(function)是指在网页DOM结构完成了执行的, 据说这样 hello()就不是一个被绑定的函数,所以不被执行, 那怎样才能让b页面执行到a页面的动作呢
下边是菜鸟的问题, js执行不到jquery里的动作a.html 是jquery
<javascript>
$(function()){ function hello(){
alert('123');
}
……省略好多function……
</javascript>b.html 是js
parent.window.hello();以上的代码,a.html用js的话是可以执行的, 但改用jquery就不行了, jquery 一开始的$(function)是指在网页DOM结构完成了执行的, 据说这样 hello()就不是一个被绑定的函数,所以不被执行, 那怎样才能让b页面执行到a页面的动作呢
作用域的问题。你若想你那些个好多的function可以在页面内被访问。你有以下两种方式
1:将你的function移出$(function(){})
2:在$(function(){})内部对所有事件执行对应的绑定
是啊, 这么巧啊~(/奸笑) 其实,想私信来着,结果还是来发帖吧~ 不好意思了都, 而且解决个问题就多点经验, 也能留个记号。那个移出$()我试过 那要在外边的话 就用不到$()里的var了 我$()里边的function一些动作是连在一起的。
你的第二个方法 怎么给他们绑定呢?
function fn1(){
alert('fn1');
}
var Div=$("#divid");
Div.click(fn1);类似于这个样子
})但2楼的朋友说的是极对的。像这种,你写到自己自定义的对象中就可以了。然后这里有需要再调用就是了
function fn1(){
alert('fn1');
}$(function(){
var Div=$("#divid");
Div.click(fn1);类似于这个样子
})这样也是一样的呀。我估计没几个人会把这种fn也写到这里面来。jquery之所以用它。一是因为它的选择器,二是因为一些插件。
哦哦哦, 听你们这么一说我才知道, 其实我用jquery也就是用到插件 还没了解太深,皮毛都算不上。 只是用到jquery的动画效果和无刷新传值。。 然后我把它写到$()里 是看别人用jquery都是把代码放到$()里边的,其实我也不知道怎么用, 就知道$()是指在网页DOM结构完成执行的, 类似于window.onload,不过window.onload只能一次,$()可以多个而已
$(function(){}):表示当前页面的DOM加载完成后即执行,但如果有图片,图片不一定也加载完成了的
如果你说的内部绑定是这样的话, 那我应该是绑定了的, 因为我在$()里有这么一句
$("#nextsong").click(nextsong);
而它也是可以执行的, 在当前页面的话, 但是我b.html里用parent.window.nextsong();还是不行
好纠结, 制服不了丫的~~ 去掉$()不行, 放到$()外边也不行(是我不会放吧)。。 抓狂, 怎样才能让b页面执行到a页面的function...........
var prevCache=-1,
index=0,
currentIndex=index,
data=$("#playlist a"),
len=data.length;function show(cIndex,pIndex){
$("#player").attr("src","player.php?u="+data[cIndex].title+'&ms='+data[cIndex].target);
$("#now_playing").html(data[cIndex].target);
$(document).attr("title", data[cIndex].target);
currentIndex=cIndex;
if(pIndex!==undefined){
$(".playerpanel-prev-song-video img").attr("src",data[pIndex].href);
$(".tooltip a").html(" "+data[pIndex].target+" ");
$("#playerpanel-prev-song").fadeIn(200).animate({left:"0"},2000);
}
else{
$("#playerpanel-prev-song").animate({left:"153px"},2000).fadeOut(200);
}}
function prevsong(){
if(prevCache==-1){
return;
}
show(prevCache);
prevCache=-1;
}
function nextsong(){
prevCache=currentIndex;
index++;
index%=len;
show(index,prevCache);
}
$("#playerpanel-prev-song").hover(
function () {
$(".playerpanel-prev-song-overlay").fadeIn(300);
$(".tooltip").css("display","block");
},
function () {
$(".playerpanel-prev-song-overlay").fadeOut(300);
$(".tooltip").css("display","none");
}
);
show(index);
$("#prevsong").click(prevsong);
$("#nextsong").click(nextsong);
$("#playerpanel-prev-song").click(prevsong);
});
试试吧,有问题告诉我
var MusicBox=function(){
this.prevCache=-1;
this.index=0;
this.currentIndex=index;
this.data=$("#playlist a");
this.len=data.length;
this.show=function(cIndex,pIndex){
$("#player").attr("src","player.php?u="+this.data[cIndex].title+'&ms='+this.data[cIndex].target);
$("#now_playing").html(this.data[cIndex].target);
$(document).attr("title", this.data[cIndex].target);
this.currentIndex=cIndex;
if(pIndex!==undefined){
$(".playerpanel-prev-song-video img").attr("src",this.data[pIndex].href);
$(".tooltip a").html(" "+this.data[pIndex].target+" ");
$("#playerpanel-prev-song").fadeIn(200).animate({left:"0"},2000);
}else{
$("#playerpanel-prev-song").animate({left:"153px"},2000).fadeOut(200);
}
};
this.prevsong=function(){
if(this.prevCache==-1){return}
this.show(prevCache);
this.prevCache=-1;
};
this.nextsong=function(){
this.prevCache=currentIndex;
this.index++;
this.index%=this.len;
show(this.index,this.prevCache);
};
this.init=function(){
$("#playerpanel-prev-song").hover(
function (){
$(".playerpanel-prev-song-overlay").fadeIn(300);
$(".tooltip").css("display","block");
},
function (){
$(".playerpanel-prev-song-overlay").fadeOut(300);
$(".tooltip").css("display","none");
}
);
this.show(this.index);
$("#prevsong").click(this.prevsong);
$("#nextsong").click(this.nextsong);
$("#playerpanel-prev-song").click(this.prevsong);
}
}调用:var myBox=new MusicBox();//这一句,你可以和上面那段代码放去一个js文件里面,这样其他地方调用的时候就不用再写了$(function(){
myBox.init();
})
this.prevCache=-1;
this.index=0;
this.currentIndex=this.index;
this.data=$("#playlist a");
this.len=this.data.length;
this.show=function(cIndex,pIndex){
$("#player").attr("src","player.php?u="+this.data[cIndex].title+'&ms='+this.data[cIndex].target);
$("#now_playing").html(this.data[cIndex].target);
$(document).attr("title", this.data[cIndex].target);
this.currentIndex=cIndex;
if(pIndex!==undefined){
$(".playerpanel-prev-song-video img").attr("src",this.data[pIndex].href);
$(".tooltip a").html(" "+this.data[pIndex].target+" ");
$("#playerpanel-prev-song").fadeIn(200).animate({left:"0"},2000);
}else{
$("#playerpanel-prev-song").animate({left:"153px"},2000).fadeOut(200);
}
};
this.prevsong=function(){
if(this.prevCache==-1){return}
this.show(this.prevCache);
this.prevCache=-1;
};
this.nextsong=function(){
this.prevCache=this.currentIndex;
this.index++;
this.index%=this.len;
show(this.index,this.prevCache);
};
this.init=function(){
$("#playerpanel-prev-song").hover(
function (){
$(".playerpanel-prev-song-overlay").fadeIn(300);
$(".tooltip").css("display","block");
},
function (){
$(".playerpanel-prev-song-overlay").fadeOut(300);
$(".tooltip").css("display","none");
}
);
this.show(this.index);
$("#prevsong").click(this.prevsong);
$("#nextsong").click(this.nextsong);
$("#playerpanel-prev-song").click(this.prevsong);
}
}
this.prevCache=-1;
this.index=0;
this.currentIndex=this.index;
this.data;//在init中初始化时赋值
this.len=0;//在init中初始化时赋值
this.show=function(cIndex,pIndex){
$("#player").attr("src","player.php?u="+this.data[cIndex].title+'&ms='+this.data[cIndex].target);
$("#now_playing").html(this.data[cIndex].target);
$(document).attr("title", this.data[cIndex].target);
this.currentIndex=cIndex;
if(pIndex!==undefined){
$(".playerpanel-prev-song-video img").attr("src",this.data[pIndex].href);
$(".tooltip a").html(" "+this.data[pIndex].target+" ");
$("#playerpanel-prev-song").fadeIn(200).animate({left:"0"},2000);
}else{
$("#playerpanel-prev-song").animate({left:"153px"},2000).fadeOut(200);
}
};
this.prevsong=function(){
if(this.prevCache==-1){return}
this.show(this.prevCache);
this.prevCache=-1;
};
this.nextsong=function(){
this.prevCache=this.currentIndex;
this.index++;
this.index%=this.len;
show(this.index,this.prevCache);
};
this.init=function(){
this.data=$("#playlist a");
this.len=this.data.length;
$("#playerpanel-prev-song").hover(
function (){
$(".playerpanel-prev-song-overlay").fadeIn(300);
$(".tooltip").css("display","block");
},
function (){
$(".playerpanel-prev-song-overlay").fadeOut(300);
$(".tooltip").css("display","none");
}
);
this.show(this.index);
$("#prevsong").click(this.prevsong);
$("#nextsong").click(this.nextsong);
$("#playerpanel-prev-song").click(this.prevsong);
}
}
}
这里就是js调用jquery 函数
this.prevCache=-1;
this.index=0;
this.currentIndex=this.index;
this.data;//在init中初始化时赋值
this.len=0;//在init中初始化时赋值
this.show=function(cIndex,pIndex){
$("#player").attr("src","player.php?u="+this.data[cIndex].title+'&ms='+this.data[cIndex].target);
$("#now_playing").html(this.data[cIndex].target);
$(document).attr("title", this.data[cIndex].target);
this.currentIndex=cIndex;
if(pIndex!==undefined){
$(".playerpanel-prev-song-video img").attr("src",this.data[pIndex].href);
$(".tooltip a").html(" "+this.data[pIndex].target+" ");
$("#playerpanel-prev-song").fadeIn(200).animate({left:"0"},2000);
}else{
$("#playerpanel-prev-song").animate({left:"153px"},2000).fadeOut(200);
}
};
this.prevsong=function(){
if(this.prevCache==-1){return}
this.show(this.prevCache);
this.prevCache=-1;
};
this.nextsong=function(){
this.prevCache=this.currentIndex;
this.index++;
this.index%=this.len;
this.show(this.index,this.prevCache);
};
this.init=function(){
this.data=$("#playlist a");
this.len=this.data.length;
$("#playerpanel-prev-song").hover(
function (){
$(".playerpanel-prev-song-overlay").fadeIn(300);
$(".tooltip").css("display","block");
},
function (){
$(".playerpanel-prev-song-overlay").fadeOut(300);
$(".tooltip").css("display","none");
}
);
this.show(this.index);
var _that=this;
$("#prevsong").click(function(){_that.prevsong()});
$("#nextsong").click(function(){_that.nextsong()});
$("#playerpanel-prev-song").click(function(){_that.prevsong()});
}
}
额~~ 我狂汗~ 可以了.. 不过和之前一样的原因 你的改法我一点不懂 感觉乱所以才再找找有什么办法没, 更重要的是 我要加ajax获重新获取列表的话,还是要写在那段代码里, 不好修改。
不过眼下的问题都解决了, 在当前页面写上function nextsong(){myBox.nextsong(); } 然后b页面就可以执行了, 非常感谢, 等我多玩玩以后懂的多了再优化下吧。
var MusicBox=function(){
this.prevCache=-1;
this.index=0;
this.currentIndex=this.index;
this.data;//在init中初始化时赋值
this.len=0;//在init中初始化时赋值
this.show=function(cIndex,pIndex){
$("#player").attr("src","player.php?u="+this.data.eq(cIndex).attr("title")+'&ms='+this.data.eq(cIndex).attr("target"));
$("#now_playing").html(this.data.eq(cIndex).attr("target"));
$(document).attr("title", this.data.eq(cIndex).attr("target"));
this.currentIndex=cIndex;
if(pIndex!==undefined){
$(".playerpanel-prev-song-video img").attr("src",this.data.eq(pIndex).attr("href"));
$(".tooltip a").html(" "+this.data.eq(pIndex).attr("target")+" ");
$("#playerpanel-prev-song").fadeIn(200).animate({left:"0"},2000);
}else{
$("#playerpanel-prev-song").animate({left:"153px"},2000).fadeOut(200);
}
};
this.prevsong=function(){
if(this.prevCache==-1){return}
this.show(this.prevCache);
this.prevCache=-1;
};
this.nextsong=function(){
this.prevCache=this.currentIndex;
this.index++;
this.index%=this.len;
this.show(this.index,this.prevCache);
};
this.init=function(){
this.data=$("#playlist a");
this.len=this.data.size();
$("#playerpanel-prev-song").hover(
function (){
$(".playerpanel-prev-song-overlay").fadeIn(300);
$(".tooltip").css("display","block");
},
function (){
$(".playerpanel-prev-song-overlay").fadeOut(300);
$(".tooltip").css("display","none");
}
);
this.show(this.index);
var _that=this;
$("#prevsong").click(function(){_that.prevsong()});
$("#nextsong").click(function(){_that.nextsong()});
$("#playerpanel-prev-song").click(function(){_that.prevsong()});
}
}
var myBox=new MusicBox();//顺序不能乱,这个必须在匿名函数的外部
$(function(){
myBox.init();//初始化时必须在匿名函数的内容部
})
你说的b页面,b如果是使用iframe嵌入当前页面的话,那么使用
parent.myBox.nextsong();//即可播放下一首
parent.myBox.prevsong();//即可播放上一首
parent.myBox.init();//即可重新初始化
$("#playerpanel-prev-song").click(function(){_that.prevsong()});
上面两句应该可以合起来写
$("#prevsong,#playerpanel-prev-song").click(function(){_that.prevsong()});
var MusicBox=function(){
//自定义的MusicBox
}
var myBox=new MusicBox();//在匿名函数的外部构造MusicBox函数,创建了一个实例。使得当前页面可以用myBox去访问MusicBox的变量和属性
$(function(){
myBox.init();//在匿名函数的内部进行初始化,可以使得MusicBox在当前页面DOM结构完整的前提下执行相对应的操作,或者会找不着对象
})至于你说的ajax,将下面的代码url部份改成你自己的,然后在获取数据后执行你的操作即可
$.ajax({
type: "get",
cache: false,
url: "页面地址?参数1=1&参数2=2",
beforeSend: function () {},
success: function (data) {
//data即为你请求页面返回的数据
//你可以在这里执行获取数据后的操作
//因为是在MusicBox内部,调用对应函数:this.函数名称
},
error: function (http) {alert('发生一个错误')},
complete: function (http) { }
});
嗯, 多谢你啦~ 我b页面是嵌套的, 是我不懂代码啦, 我整代码 只要能运行就好了 根本没管错没错, 因为我想能运行的话就没错吧, 或者可以说 其实就算它能执行了我也不知道它是为什么能执行, 所以我才是个不懂编程的人, 让我自己完整的写小段就简单的alert 我都不会, 不像你们, 都会的 现在也许都从事IT行业的吧, 一写就是完整的系统式的一段代码, 所以我真的很开心能在这里遇见你们这些热心的朋友, 这问题解决了, 结贴了。
貌似我从问第一个都现在都是你帮我~~ 而且从第一个问题到这个问题, 根本就是一个问题~~ 典型的不懂的新手呀~~ Good night friends!