Javascript 是否对元素类更改运行jQuery回调?
下面是我正在尝试使用的当前jQuery代码Javascript 是否对元素类更改运行jQuery回调?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,下面是我正在尝试使用的当前jQuery代码 $("ul.nav.nav-tabs li.active a").each(function(){ switch(this.hash) { case "#core": alert("Core"); break; case "#parallel": alert("Parallel");
$("ul.nav.nav-tabs li.active a").each(function(){
switch(this.hash) {
case "#core":
alert("Core");
break;
case "#parallel":
alert("Parallel");
break;
case "#cron_settings":
alert("Cron Settings");
break;
}
})
下面是我正在使用的HTML标记
<ul class="nav nav-tabs">
<li class="active"><a href="#core" data-toggle="tab">Core Settings</a></li>
<li><a href="#parallel" data-toggle="tab">Parallel settings</a></li>
<li><a href="#cron_settings" data-toggle="tab">Cron limit settings</a></li>
</ul>
当active
类更改时,我需要它基本上重新运行switch()
,因为它只在文档上执行一次。就绪
,如何让它重新运行switch()
?可能使用change()
?这可能有效
<script>
$(function(){
// Bind the event.
$(window).on("hashchange",function(){
// Alerts every time the hash changes!
alert( "call you function");
// your switch..
})
});
</script>
$(函数(){
//绑定事件。
$(window).on(“hashchange”,function()){
//每次哈希更改时都会发出警报!
警报(“呼叫您功能”);
//你的开关。。
})
});
这可能有效
<script>
$(function(){
// Bind the event.
$(window).on("hashchange",function(){
// Alerts every time the hash changes!
alert( "call you function");
// your switch..
})
});
</script>
$(函数(){
//绑定事件。
$(window).on(“hashchange”,function()){
//每次哈希更改时都会发出警报!
警报(“呼叫您功能”);
//你的开关。。
})
});
这可能有效
<script>
$(function(){
// Bind the event.
$(window).on("hashchange",function(){
// Alerts every time the hash changes!
alert( "call you function");
// your switch..
})
});
</script>
$(函数(){
//绑定事件。
$(window).on(“hashchange”,function()){
//每次哈希更改时都会发出警报!
警报(“呼叫您功能”);
//你的开关。。
})
});
这可能有效
<script>
$(function(){
// Bind the event.
$(window).on("hashchange",function(){
// Alerts every time the hash changes!
alert( "call you function");
// your switch..
})
});
</script>
$(函数(){
//绑定事件。
$(window).on(“hashchange”,function()){
//每次哈希更改时都会发出警报!
警报(“呼叫您功能”);
//你的开关。。
})
});
根据先前建议“hashchange”活动的海报,这里是您想要的工作副本:
$('li').on('click', function() {
$('li').removeClass('active');
$(this).addClass('active');
// UNNECESSARY: window.location.hash=$('a', this).attr('href');
});
$(window).on('hashchange', function(){
switch(window.location.hash) {
case "#core":
alert("Core");
break;
case "#parallel":
alert("Parallel");
break;
case "#cron_settings":
alert("Cron Settings");
break;
}
});
根据先前建议“hashchange”事件的海报,这里是您想要的工作副本:
$('li').on('click', function() {
$('li').removeClass('active');
$(this).addClass('active');
// UNNECESSARY: window.location.hash=$('a', this).attr('href');
});
$(window).on('hashchange', function(){
switch(window.location.hash) {
case "#core":
alert("Core");
break;
case "#parallel":
alert("Parallel");
break;
case "#cron_settings":
alert("Cron Settings");
break;
}
});
根据先前建议“hashchange”事件的海报,这里是您想要的工作副本:
$('li').on('click', function() {
$('li').removeClass('active');
$(this).addClass('active');
// UNNECESSARY: window.location.hash=$('a', this).attr('href');
});
$(window).on('hashchange', function(){
switch(window.location.hash) {
case "#core":
alert("Core");
break;
case "#parallel":
alert("Parallel");
break;
case "#cron_settings":
alert("Cron Settings");
break;
}
});
根据先前建议“hashchange”事件的海报,这里是您想要的工作副本:
$('li').on('click', function() {
$('li').removeClass('active');
$(this).addClass('active');
// UNNECESSARY: window.location.hash=$('a', this).attr('href');
});
$(window).on('hashchange', function(){
switch(window.location.hash) {
case "#core":
alert("Core");
break;
case "#parallel":
alert("Parallel");
break;
case "#cron_settings":
alert("Cron Settings");
break;
}
});
我不熟悉“hashchange”事件的浏览器兼容性 我认为这是考虑过度了。有两个相当简单的解决方案 最简单的方法是假设活动链接将根据用户单击这些链接而改变。在这种情况下,只需使用onClick触发器重新运行脚本
var FooBar = function(){
$("ul.nav.nav-tabs li.active a").each(function(){
switch(this.hash) {
case "#core":
alert("Core");
break;
case "#parallel":
alert("Parallel");
break;
case "#cron_settings":
alert("Cron Settings");
break;
}
});
};
FooBar();//run the function once initially
$("ul.nav.nav-tabs").each(function(){
$(this).click=FooBar;
});
或者,如果可以通过javascript修改“active”类,则可以在该代码中调用FooBar。如果无法修改该javascript,则可以使用超时来解决此问题
setInterval(FooBar, 500);//Run the switch function every half second
我不确定你到底想完成什么,我想可能有一个更简单的方法
编辑:
您可以使用由Thalis Kalfigkopulos根据答案修改的代码。
这将直接从on click事件运行switch语句:
$('li').on('click', function(e) {
$('li').removeClass('active');
$(this).addClass('active');
switch($(this).children('a').prop('hash')) {
case "#core":
alert("Core");
break;
case "#parallel":
alert("Parallel");
break;
case "#cron_settings":
alert("Cron Settings");
break;
}
});
我不熟悉“hashchange”事件的浏览器兼容性 我认为这是考虑过度了。有两个相当简单的解决方案 最简单的方法是假设活动链接将根据用户单击这些链接而改变。在这种情况下,只需使用onClick触发器重新运行脚本
var FooBar = function(){
$("ul.nav.nav-tabs li.active a").each(function(){
switch(this.hash) {
case "#core":
alert("Core");
break;
case "#parallel":
alert("Parallel");
break;
case "#cron_settings":
alert("Cron Settings");
break;
}
});
};
FooBar();//run the function once initially
$("ul.nav.nav-tabs").each(function(){
$(this).click=FooBar;
});
或者,如果可以通过javascript修改“active”类,则可以在该代码中调用FooBar。如果无法修改该javascript,则可以使用超时来解决此问题
setInterval(FooBar, 500);//Run the switch function every half second
我不确定你到底想完成什么,我想可能有一个更简单的方法
编辑:
您可以使用由Thalis Kalfigkopulos根据答案修改的代码。
这将直接从on click事件运行switch语句:
$('li').on('click', function(e) {
$('li').removeClass('active');
$(this).addClass('active');
switch($(this).children('a').prop('hash')) {
case "#core":
alert("Core");
break;
case "#parallel":
alert("Parallel");
break;
case "#cron_settings":
alert("Cron Settings");
break;
}
});
我不熟悉“hashchange”事件的浏览器兼容性 我认为这是考虑过度了。有两个相当简单的解决方案 最简单的方法是假设活动链接将根据用户单击这些链接而改变。在这种情况下,只需使用onClick触发器重新运行脚本
var FooBar = function(){
$("ul.nav.nav-tabs li.active a").each(function(){
switch(this.hash) {
case "#core":
alert("Core");
break;
case "#parallel":
alert("Parallel");
break;
case "#cron_settings":
alert("Cron Settings");
break;
}
});
};
FooBar();//run the function once initially
$("ul.nav.nav-tabs").each(function(){
$(this).click=FooBar;
});
或者,如果可以通过javascript修改“active”类,则可以在该代码中调用FooBar。如果无法修改该javascript,则可以使用超时来解决此问题
setInterval(FooBar, 500);//Run the switch function every half second
我不确定你到底想完成什么,我想可能有一个更简单的方法
编辑:
您可以使用由Thalis Kalfigkopulos根据答案修改的代码。
这将直接从on click事件运行switch语句:
$('li').on('click', function(e) {
$('li').removeClass('active');
$(this).addClass('active');
switch($(this).children('a').prop('hash')) {
case "#core":
alert("Core");
break;
case "#parallel":
alert("Parallel");
break;
case "#cron_settings":
alert("Cron Settings");
break;
}
});
我不熟悉“hashchange”事件的浏览器兼容性 我认为这是考虑过度了。有两个相当简单的解决方案 最简单的方法是假设活动链接将根据用户单击这些链接而改变。在这种情况下,只需使用onClick触发器重新运行脚本
var FooBar = function(){
$("ul.nav.nav-tabs li.active a").each(function(){
switch(this.hash) {
case "#core":
alert("Core");
break;
case "#parallel":
alert("Parallel");
break;
case "#cron_settings":
alert("Cron Settings");
break;
}
});
};
FooBar();//run the function once initially
$("ul.nav.nav-tabs").each(function(){
$(this).click=FooBar;
});
或者,如果可以通过javascript修改“active”类,则可以在该代码中调用FooBar。如果无法修改该javascript,则可以使用超时来解决此问题
setInterval(FooBar, 500);//Run the switch function every half second
我不确定你到底想完成什么,我想可能有一个更简单的方法
编辑:
您可以使用由Thalis Kalfigkopulos根据答案修改的代码。
这将直接从on click事件运行switch语句:
$('li').on('click', function(e) {
$('li').removeClass('active');
$(this).addClass('active');
switch($(this).children('a').prop('hash')) {
case "#core":
alert("Core");
break;
case "#parallel":
alert("Parallel");
break;
case "#cron_settings":
alert("Cron Settings");
break;
}
});
元素属性更改没有跨浏览器事件取决于您正在测试的浏览器,您可能还会发现有趣的
domatrtmodified
事件。请记住,并非所有浏览器都支持它。有关详细信息,请参阅。元素属性更改没有跨浏览器事件取决于您正在测试的浏览器,您可能还会发现有趣的domatrtmodified
事件。请记住,并非所有浏览器都支持它。有关详细信息,请参阅。元素属性更改不存在跨浏览器事件,取决于您使用的浏览器