javascript - 如何将此 jQuery 实现到 HTML 文档中?
问题描述
我正在尝试实现有人从另一篇文章中给我的这个片段,它完美地工作,但我不知道插入它的正确方法。
我正在通过 Elementor 块生成器在 Wordpress 上工作,并有一个自定义 HTML 小部件,我正在尝试将此代码插入其中。
我是一个刚刚开始使用 JavaScript 和 jQuery 的新手。在 StackOverflow 上运行它非常完美,因为我可以将 HTML 放在 HTML 部分,将 jQuery 放在 JavaScript 部分,但显然我不能在 Wordpress 上运行。
任何帮助将非常感激!
jQuery(function($) {
$("#image1").click(function() {
$(this).attr("src", "https://lowlifeclothing.co/wp-content/uploads/2020/11/Loop12D.gif");
var url = $(this).attr("data-click-href");
setTimeout(function() {
window.location.href = url;
}, 1000);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="https://lowlifeclothing.co/wp-content/uploads/2020/11/Loop12.gif" data-click-href="https://lowlifeclothing.co/shop/" id="image1">
解决方案
JQuery 已经在 Wordpress 中排队。您不需要包含 <script src=''...jquery'/>
您可以使用 Elementor 的 HTML 块并添加您的 HTML 代码。Javascript 代码需要包含在 script 标签中。
<script>
jQuery(function($) {
$("#image1").click(function() {
$(this).attr("src", "https://lowlifeclothing.co/wp-content/uploads/2020/11/Loop12D.gif");
var url = $(this).attr("data-click-href");
setTimeout(function() {
window.location.href = url;
}, 1000);
});
});
</script>
<img src="https://lowlifeclothing.co/wp-content/uploads/2020/11/Loop12.gif" data-click-href="https://lowlifeclothing.co/shop/" id="image1">
现在,当您单击图像时,鹿会装死!
查看此图像以获取参考代码示例 -
https://askteammate.com/wp-content/uploads/2020/11/StackOverFlow-Question-64960881.png
推荐阅读
- python - 为什么通过“acquire_token_with_username_password”获取 Azure AD 令牌失败?
- node.js - 更新和删除请求在 reactjs、nodejs(express) 和 dynamodb 中出现 CORS 错误
- php - 在 PHP 中进行总计或计数循环
- python-3.x - 使用本地 IDE(例如 Microsoft 可视化代码)来使用额外的计算
- google-cloud-platform - 云机器内部不存在谷歌云凭证
- postgresql - PostgreSQL 和 TSTZRANGE
- javascript - 通过Javascript定期自动单击列表项
- windows - 为什么`chrome.notifications.update` 在 Windows 10 上只更新一次?
- python - Flask 会话值不会保存在不同的请求中
- php - 如果相同日期,则 CSV 覆盖行,否则附加到新行