简体   繁体   English

Javascript 函数未在点击事件上定义,但适用于页面加载?

[英]Javascript Function Not Defined On Click Event, But Works on Page Load?

I have a Wordpress site set up on my local machine where I'm building a custom theme.我在本地机器上设置了一个 Wordpress 站点,我正在其中构建自定义主题。 In an 'enqueue-assets.php' file, I am using wp_enqueue_script to load a main bundle javascript file.在 'enqueue-assets.php' 文件中,我使用 wp_enqueue_script 加载主包 javascript 文件。 In that bundle are additional imports of other .js files.在该包中是其他 .js 文件的额外导入。 In one of the imported .js files is the following function:在导入的 .js 文件之一中,有以下函数:

function svgPageJump(target){
    alert(target);
}

I also tried assigning the function to variable, just to make sure I got the same results explained below:我还尝试将函数分配给变量,只是为了确保得到与下面解释的结果相同的结果:

let svgPageJump = function(target){
    alert(target);
};

and

var svgPageJump = function(target){
    alert(target);
};

When I call this function in the same .js file with .ready():当我使用 .ready() 在同一个 .js 文件中调用这个函数时:

$(document).ready(function(){
    svgPageJump('section-plan');
});

I get the alert when the page loads, as expected.正如预期的那样,我在页面加载时收到警报。 I also get the alert by just calling it after the function declaration (also as expected.)我也通过在函数声明之后调用它来获得警报(也如预期的那样。)

Unfortunately this isn't working on click events elsewhere on the page.不幸的是,这不适用于页面上其他地方的点击事件。 Ultimately I'm trying to get these events to fire when clicking elements in a SVG, to create page jumps.最终,我试图在单击 SVG 中的元素时触发这些事件,以创建页面跳转。 On the clickable elements I'm using:在我使用的可点击元素上:

onclick="top.svgPageJump('section-plan');"

Clicking on the element gives me this error:单击该元素给我这个错误:

Uncaught TypeError: top.svgPageJump is not a function
    at SVGGElement.onclick

At first I thought it was a targeting issue from the SVG, but I did another test on a bit of text elsewhere on the page.起初我认为这是 SVG 的定位问题,但我对页面其他地方的一些文本进行了另一次测试。 In a Gutenburg Heading Block in a Wordpress Page, I placed the following markup using the HTML editor for the block:在 Wordpress 页面的古腾堡标题块中,我使用该块的 HTML 编辑器放置了以下标记:

<h2 id="section-plan" onclick="svgPageJump('section-plan');">PLAN</h2>

I also did this in a Custom HTML Block, but either way when I click the h2 on the actual page I get this error:我也在自定义 HTML 块中执行此操作,但是无论哪种方式,当我单击实际页面上的 h2 时,都会出现此错误:

Uncaught ReferenceError: svgPageJump is not defined
    at HTMLHeadingElement.onclick

I'm obviously missing something (probably something simple) but not sure how to track the problem down between the theme's files (which are all working correctly otherwise) or the event being defined in the page via the Wordpress admin, or...?我显然遗漏了一些东西(可能是一些简单的东西),但不确定如何在主题文件(否则它们都可以正常工作)或通过 Wordpress 管理员在页面中定义的事件之间跟踪问题,或者...?

Another note on this... I originally used the simpler approach to making these page jumps using anchor tags and associated ids.关于此的另一个注意事项...我最初使用更简单的方法使用锚标记和关联的 id 使这些页面跳转。 This worked fine but I have a sticky header on the page so when the page scrolls, the element with the id scrolls under the header so I am attempting to use JavaScript/jQuery to account for the header height.这工作正常,但我在页面上有一个粘性标题,因此当页面滚动时,带有 id 的元素在标题下滚动,因此我尝试使用 JavaScript/jQuery 来说明标题高度。

Thanks!谢谢!

As per this question (which I'd close as a duplicate of it if has an upvoted answer):根据这个问题(如果有一个赞成的答案,我会将其作为副本关闭):

var in the top level of a non-module script will create a property on the window object.非模块脚本顶层的var将在window对象上创建一个属性。 So will using a function declaration .使用函数声明也是如此 You can access this from a frame via top .您可以通过top从框架访问它。

let does not create a property on the window object, so you can't access it that way if you use let . let不会window对象上创建属性,因此如果您使用let ,则无法以这种方式访问​​它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM