简体   繁体   English

如何使用JavaScript从插件中获取osclass主题HTML?

[英]How can I grab osclass theme HTML from plugin using JavaScript?

I have a variable called btn that I assign to a document.querySelector() function. 我有一个名为btn的变量,我将其分配给document.querySelector()函数。 I know the query selection is right because it grabs the element I want when I run it through the browser console. 我知道查询选择是正确的,因为当我通过浏览器控制台运行它时,它会抓取我想要的元素。 Nonetheless, the assignment produces a null result. 但是,赋值会产生null结果。 Is this because I cannot grab HTML in this way with osclass framework? 这是因为我不能用osclass框架以这种方式获取HTML吗?

I'm developing a plugin. 我正在开发一个插件。 This plugin alters the registration form. 此插件会更改注册表单。 I'm adding a text field wherein I've used an event listener to detect input. 我正在添加一个文本字段,其中我使用了一个事件监听器来检测输入。 If there's input, I want the form to do something when the submit button is clicked. 如果有输入,我希望表单在单击提交按钮时执行某些操作。 There's no problem detecting this form field, I suspect because I've just added it in the plugin folder. 检测这个表单字段没有问题,我怀疑是因为我刚刚将它添加到插件文件夹中。 I cannot, however, grab HTML from the theme. 但是,我无法从主题中获取HTML。 I think part of this is because I'm trying to grab it before the HTML loads. 我认为部分原因是因为我试图在HTML加载之前抓住它。 Even so, I've tried deferring the script and using an inline script to run the code after the DOM content loads. 即便如此,我已经尝试推迟脚本并使用内联脚本在DOM内容加载后运行代码。 No success. 没有成功。

Here's the bit of HTML I want to grab. 这是我想要抓取的HTML。

<div class="controls">
     <button type="submit" class="ui-button ui-button-middle ui-button-main"><?php _e("Create", 'bender'); ?></button>
</div>

Here's the code I'm using to do that, and the code I want to execute: 这是我用来做的代码,以及我想要执行的代码:

var btn = document.querySelector("div.controls button");
btn.addEventListener("submit", function() {
     console.log("Honey is sweet!");
});

Finally here's how I'm running the script: 最后,这是我运行脚本的方式:

<script type="text/javascript" async="" defer="" src="<?php echo osc_base_url().'oc-content/plugins/honeypot/fieldcheck.js';?>"></script>

I should mention I'm running this as a function with the osclass hook: osc_add_hook('user_register_form', 'twf_honeypot_form_field'); 我应该提一下,我将这个作为函数与osclass钩子一起运行: osc_add_hook('user_register_form', 'twf_honeypot_form_field');

I expect the variable to grab the button, and the event listener to be added to to the button. 我希望变量能够抓取按钮,并将事件监听器添加到按钮中。 Once the form is submitted, it should log the message in the console. 提交表单后,它应该在控制台中记录消息。 What's happening instead is this error: Uncaught TypeError: Cannot read property 'addEventListener' of null. 发生的是这个错误:Uncaught TypeError:无法读取null的属性'addEventListener'。

If you've used osclass, is this because the HTML lives elsewhere? 如果您使用过osclass,这是因为HTML存在于其他地方吗? I thought that, if I deferred the script, this code wouldn't execute until the page loaded, and I'd still be able to grab the HTML. 我想,如果我推迟了脚本,这个代码在页面加载之前就不会执行,我仍然可以抓取HTML。 If not, how can I grab this bit of HTML from a theme file? 如果没有,我如何从主题文件中获取这些HTML?

Turns out this line was the problem: osc_add_hook('user_register_form', 'twf_honeypot_form_field') . 事实证明这一行是问题所在: osc_add_hook('user_register_form', 'twf_honeypot_form_field') The code wouldn't execute before the dom loaded, even if I specified for it to do so in the script itself because the script still executed only here. 代码在dom加载之前不会执行,即使我在脚本本身指定了它,因为脚本仍然只在这里执行。 I put the script call in a function and added a new hook: osc_add_hook('after_html', 'twf_deny'); 我把脚本调用放在一个函数中并添加了一个新的钩子: osc_add_hook('after_html', 'twf_deny'); Should have guessed there was a hook for that use case. 本来应该猜到那个用例有一个钩子。

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

相关问题 如何使用javascript从页面代码中获取值? - How can I grab a value from the code of the page using javascript? 如何获取 HTML 滑块值作为 JavaScript 的整数? - How can I grab an HTML slider value as an integer for JavaScript? 如何使用JavaScript从HTML表格的单元格中获取值 - How to grab the values from an HTML table's cells using JavaScript 如何使用javaScript从网站(页面源)中的href抓取文本? - how can i grab a text from a href in a website (page source) using javaScript? 如何获取 html 文件中的输入并将其从 javascript 文件中获取到 - how to grab an input in an html file and grab it from javascript file to 如何从Chrome的JavaScript控制台中获取错误 - How can I grab the error from javascript console in chromium 如何使用 javascript 从 URL 中获取特定 ID - How do I grab a specific ID from a URL using javascript 如何使用JavaScript从URL抓取任何句段? - How do I grab any segment from URL using Javascript? 如何从 PHP 生成的 div 中获取值并使用 Javascript 然后更改进度条 HTML5 元素的宽度? - How can I grab a value from a PHP generated div and use Javascript to then change the width of a progress bar HTML5 element? 如何从输入框中获取一些输入,放置在变量中,然后使用HTML和Javascript输出到范围ID? - How do I grab a few inputs from input boxes, place in variables, then output to a span id using HTML & Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM