在 ID 不等于 URL 最后一部分的跨度上触发点击
Trigger Click On Span Without ID Equal To Last Part Of URL
如何在没有 id 的 span 上触发点击事件?拒绝触发的相关跨度是 ID 等于“”的跨度。当我单击浏览器上的后退或前进按钮时,它不会执行任何操作,而所有其他按钮都会触发加载相应内容的单击事件。
<span id=""></span>
<span id="last_part_match"></span>
这就是我想要做的,但它不起作用并且破坏了代码。我的原始代码只是在 last_part ID 上触发了触发器,但这并没有起作用,因为 id = "".
window.addEventListener("popstate", function() {
var url = $(location).attr('href');
var parts = url.split("/");
var last_part = parts[parts.length-1];
if((".dashboard_post_nav span").attr('id') == last_part) {
$("#" + last_part).trigger("click");
} else {
$(".dashboard_post_nav span:first-of-type").trigger("click");
}
});
这是执行历史推送的代码,以备不时之需。 dash_switch 只是在点击事件中适当加载正确内容的选择器。
if(!e.isTrigger) {
if(history.pushState) {
if(dash_switch == '') {
history.pushState(null, null, '/');
} else {
history.pushState(null, null, '/' + dash_url + '/' + dash_switch + '');
}
} else {
if(dash_switch == '') {
location.hash = '/';
} else {
location.hash = '/' + dash_url + '/' + dash_switch + '';
}
}
}
而不是使用 attr()
使用 id 作为选择器并检查结果的长度
if( $('#' + last_part).length ) {
如何在没有 id 的 span 上触发点击事件?拒绝触发的相关跨度是 ID 等于“”的跨度。当我单击浏览器上的后退或前进按钮时,它不会执行任何操作,而所有其他按钮都会触发加载相应内容的单击事件。
<span id=""></span>
<span id="last_part_match"></span>
这就是我想要做的,但它不起作用并且破坏了代码。我的原始代码只是在 last_part ID 上触发了触发器,但这并没有起作用,因为 id = "".
window.addEventListener("popstate", function() {
var url = $(location).attr('href');
var parts = url.split("/");
var last_part = parts[parts.length-1];
if((".dashboard_post_nav span").attr('id') == last_part) {
$("#" + last_part).trigger("click");
} else {
$(".dashboard_post_nav span:first-of-type").trigger("click");
}
});
这是执行历史推送的代码,以备不时之需。 dash_switch 只是在点击事件中适当加载正确内容的选择器。
if(!e.isTrigger) {
if(history.pushState) {
if(dash_switch == '') {
history.pushState(null, null, '/');
} else {
history.pushState(null, null, '/' + dash_url + '/' + dash_switch + '');
}
} else {
if(dash_switch == '') {
location.hash = '/';
} else {
location.hash = '/' + dash_url + '/' + dash_switch + '';
}
}
}
而不是使用 attr()
使用 id 作为选择器并检查结果的长度
if( $('#' + last_part).length ) {