如何使用窗口。
我在导航菜单中有两个不同的按钮,它们是指向主页上的标签切换器的锚点,该链接具有两个选项卡。单击时,我需要导航按钮才能更改活动选项卡。它应该是一个简单的代码,但我是JS中的新手。
我需要在页面加载后调用功能,这样,它也可以从其他页面起作用,而不仅仅是从主页上使用。
第一个函数本身可以正常工作。当单击其他页面上的按钮时,该功能也可以工作。它调用主页,然后更改页面加载后的活动选项卡:
add_action( 'wp_footer', function () { ?>
<script>
jQuery('#menu-item-15507 a').on('click', window.onload=function myfunction_1() {
jQuery( '#one-to-one a' ).click();
return true;
});
</script>
<?php } );
但是,当我添加第二个事件和功能时,这些选项卡不会在pageload上更改。它在同一页面(主页)上工作,但是从其他页面启动时不工作:
add_action( 'wp_footer', function () { ?>
<script>
jQuery('#menu-item-15507 a').on('click', window.onload=function myfunction_1() {
jQuery( '#one-to-one a' ).click();
return true;
});
jQuery('#menu-item-16848 a').on('click', window.onload=function myfunction_2() {
jQuery( '#courses a' ).click();
return true;
});
</script>
<?php } );
我应该如何更改代码以使两个功能也可以在页面加载上工作?
I have two different buttons in the navigation menu, they are anchor links to a tab switcher on the home page, which has two tabs. I need the navigation buttons to change the active tab, when clicked. It should be a simple code, but I am a newbie in JS.
I need this to call the functions after the page loads, this way, it will work from other pages too, not just from the home page.
The first function works in itself as it should. The function also works when the button is clicked on a different page. It calls the homepage, then changes the active tab after page load:
add_action( 'wp_footer', function () { ?>
<script>
jQuery('#menu-item-15507 a').on('click', window.onload=function myfunction_1() {
jQuery( '#one-to-one a' ).click();
return true;
});
</script>
<?php } );
But when I add the second event and function, the tabs are not changing on pageload. It works on the same page (home page), but not when fired from a different page:
add_action( 'wp_footer', function () { ?>
<script>
jQuery('#menu-item-15507 a').on('click', window.onload=function myfunction_1() {
jQuery( '#one-to-one a' ).click();
return true;
});
jQuery('#menu-item-16848 a').on('click', window.onload=function myfunction_2() {
jQuery( '#courses a' ).click();
return true;
});
</script>
<?php } );
How am I supposed to change the code to get both functions to work on page load as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个问题是 - 如果需要运行多个功能,请不要在 - 设置上调用
- 使用
addeventListener
而不是。但是,在单击“侦听器”中分配给
.onload
无论如何都没有任何意义 - 到用户可以单击时,窗口可能会加载。在jQuery准备页面时运行某些内容的标准方法是使用主
$
函数的回调。One problem is - don't invoke the
on-
setters if you need to run more than one function - useaddEventListener
instead.But assigning to
.onload
inside a click listener doesn't make any sense anyway - by the time the user can click, the window will likely have loaded anyway.The standard way to run something when the page is ready in jQuery is to use the main
$
function's callback.