单击选项卡时如何更新 url 链接
您好,我的网站中有三个选项卡,这些选项卡是根据 WordPress 中的 elementor 插件创建的,我想在导航到选项卡时提供 url,因为我已经编写了一些正在运行的脚本,但是当我单击一个选项卡,它会导航到不同的选项卡,您可以帮我解决这个问题吗
<script>
window.onload = function () {
let search = ""
var dmi = document.getElementById("elementor-tab-title-3201");
if(window.location.search!=="?DataMonetizationIndex"){
search = "";
dmi.onclick = changeLink;
search="?DataMonetizationIndex";
}
var dp = document.getElementById("elementor-tab-title-3203");
if(window.location.search!=="?DataProducts"){
search = "";
dp.onclick = changeLink;
search="?DataProducts";
}
var ans = document.getElementById("elementor-tab-title-3202");
if(window.location.search!=="?Analysis"){
search = "";
ans.onclick = changeLink;
search="?Analysis";
}
function changeLink() {
window.location.search=search;
}
}
document.addEventListener('DOMContentLoaded', function() {
setTimeout(function() {
jQuery(function($){
let desktoptitles = $('.elementor-tab-desktop-title');
let mobiletitles = $('.elementor-tab-mobile-title');
let strings = ['?DataMonetizationIndex',
'?Analysis','?DataProducts'
];
strings.forEach( (string,i) => {
if (window.location.href.indexOf(string) > -1) {
desktoptitles.eq(i).click();
mobiletitles.eq(i).click();
$('html, body').animate({
scrollTop: desktoptitles.eq(i).offset().top - 100
},'slow');
} } );
}); }, 1200); });
</script>
Hi i have three tabs in my website for the post these are created based on the elementor plugin in wordpress i want to provide the url when navigating to the tabs for that i have written the some script it is working but there is an issue when i clicking on the onetab it is navigating to the different tab can you help me on fixing this one
<script>
window.onload = function () {
let search = ""
var dmi = document.getElementById("elementor-tab-title-3201");
if(window.location.search!=="?DataMonetizationIndex"){
search = "";
dmi.onclick = changeLink;
search="?DataMonetizationIndex";
}
var dp = document.getElementById("elementor-tab-title-3203");
if(window.location.search!=="?DataProducts"){
search = "";
dp.onclick = changeLink;
search="?DataProducts";
}
var ans = document.getElementById("elementor-tab-title-3202");
if(window.location.search!=="?Analysis"){
search = "";
ans.onclick = changeLink;
search="?Analysis";
}
function changeLink() {
window.location.search=search;
}
}
document.addEventListener('DOMContentLoaded', function() {
setTimeout(function() {
jQuery(function($){
let desktoptitles = $('.elementor-tab-desktop-title');
let mobiletitles = $('.elementor-tab-mobile-title');
let strings = ['?DataMonetizationIndex',
'?Analysis','?DataProducts'
];
strings.forEach( (string,i) => {
if (window.location.href.indexOf(string) > -1) {
desktoptitles.eq(i).click();
mobiletitles.eq(i).click();
$('html, body').animate({
scrollTop: desktoptitles.eq(i).offset().top - 100
},'slow');
} } );
}); }, 1200); });
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不清楚你的问题是什么,但查看代码我认为问题可能出现在第一部分,在
window.onload = function () {...}
您检查 window.location.search 并分配变量搜索。问题是你有两个可能的值可以分配给一个变量。
让我用一个例子来解释我的疑问:
假设您位于“DataMonetizationIndex”选项卡中。
在您首先分配的负载上
,然后
现在,当单击选项卡并触发
changeLink()
时,全局变量 search 的值始终是第二个分配,不不管你点击哪个选项卡。 (在这个例子中它总是?分析)这可能是一个问题。
为什么不简单地将 changeLink() 分配给所有选项卡/按钮并在函数内部检查单击的选项并更新 window.location.search ?
您可以使用自定义数据属性来执行它,或者简单地检查单击元素的 ID。
我在这段代码中要做的可能是这样的函数,
这将适用于任意数量的选项卡,并且很容易实现未来的更改。
更新
应用于您的代码,可能是这样的
脚本的第二部分(查看加载哪个选项卡处于活动状态并滚动到触发单击的该项目的部分)似乎可以,
如果它解决了您的问题,请接受答案:)
Is not clear what is your problem, but looking at code i think that a problem could be in the firs part, on
window.onload = function () {...}
You check for window.location.search and you assign the variable search. The problem is that you have 2 possible value to assign in a single variable.
Let me explain my doubt with an example:
Assuming that you are in the ?DataMonetizationIndex tab.
On the load you first assing
and then
Now, when a tab is clicked, and the
changeLink()
fired, the value of global variable search it's always the second assignement, no matter wich tab are you clicking. (in this example it's always ?Analysis)This could be a problem.
Why don't you simply assing changeLink() to all tab/button and inside the function check wich one was clicked and update the window.location.search ?
You can perfom it using a custom data-attribute , or simply checking the ID of the clicked element.
What I would do in this code is probably a function like this
This will work with an arbitrary number of tab and it is easy to implement future changes.
UPDATE
Applied to your code it could be something like this
The second part of your script (that one that look on load what tab is active and scroll to that item triggering the click) seems ok
Please accept answer if it solve your problem :)