如何使用窗口。

发布于 2025-02-12 17:02:51 字数 974 浏览 4 评论 0原文

我在导航菜单中有两个不同的按钮,它们是指向主页上的标签切换器的锚点,该链接具有两个选项卡。单击时,我需要导航按钮才能更改活动选项卡。它应该是一个简单的代码,但我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铜锣湾横着走 2025-02-19 17:02:51

一个问题是 - 如果需要运行多个功能,请不要在 - 设置上调用- 使用addeventListener而不是。

但是,在单击“侦听器”中分配给.onload无论如何都没有任何意义 - 到用户可以单击时,窗口可能会加载。

在jQuery准备页面时运行某些内容的标准方法是使用主$函数的回调。

$(() => {
  const clickOne = () => $('#one-to-one a').click();
  clickOne();
  $('#menu-item-15507 a').on('click', clickOne);

  const clickCourses = () => $('#one-to-one a').click();
  clickCourses();
  $('#courses a').on('click', clickCourses);
});

One problem is - don't invoke the on- setters if you need to run more than one function - use addEventListener 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.

$(() => {
  const clickOne = () => $('#one-to-one a').click();
  clickOne();
  $('#menu-item-15507 a').on('click', clickOne);

  const clickCourses = () => $('#one-to-one a').click();
  clickCourses();
  $('#courses a').on('click', clickCourses);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文