简体   繁体   English

使用 jQuery (Wordpress) 使整个 div 可点击

[英]Making an entire div clickable using jQuery (Wordpress)

I have a wordpress installation and am trying to create a function that makes an entire div clickable based on the href within it.我有一个 wordpress 安装,正在尝试创建一个函数,根据其中的 href 使整个 div 可点击。 I've found a lot of documentation that tells me to do this.我找到了很多文档告诉我要这样做。

jQuery(".myBox").click(function($) {
    window.location = $(this).find("a").attr("href");
    return false;
});

But it's not working, nothing happens upon clicking.但它不起作用,点击后没有任何反应。 What's odd though is that if I do this just as a test奇怪的是,如果我这样做只是作为一个测试

jQuery(".myBox").click(function($) {
    window.location = "http://www.google.com"
    return false;
});

then it does work and the div will redirect to google.com, so it doesn't seem to be a problem with the way I'm implementing the script.然后它确实起作用了,div 将重定向到 google.com,所以它似乎不是我实现脚本的方式的问题。 Am I going wrong with the first method somewhere?我在某个地方用第一种方法出错了吗? Here's my HTML这是我的 HTML

<div class="myBox">
<p><a href="http://www.example.com/notworking">This whole box should link but doesn't.</a></p>
</div>

I̶n̶ ̶y̶o̶u̶r̶ ̶H̶T̶M̶L̶,̶ ̶y̶o̶u̶ ̶d̶o̶n̶'̶t̶ ̶h̶a̶v̶e̶ ̶a̶n̶ ̶e̶l̶e̶m̶e̶n̶t̶ ̶w̶i̶t̶h̶ ̶t̶h̶e̶ ̶c̶l̶a̶s̶s̶ ̶ ̶m̶y̶B̶o̶x̶ ̶.̶ (this was fixed in an edit)在HTML,̶你没有必要的元素与类̶ ̶m̶y̶B̶o̶x̶ (这是固定在一个编辑)

A couple of points though, do you really need jQuery for this?不过有几点,你真的需要jQuery吗? It seems like using a hammer to kill a fly.这就像用锤子杀死一只苍蝇。 Usually for "entirely clickable elements", you can use a relative positioned parent, absolute positioned link, and set it to the top/left and 100% width and height.通常对于“完全可点击的元素”,您可以使用relative定位的父级、 absolute定位的链接,并将其设置为顶部/左侧和 100% 宽度和高度。

 .myBox{padding:10px;border:1px solid #ccc;position: relative;} .cover{ position: absolute; top: 0; left: 0; width: 100%; height: 100%; } .cover:hover { background:rgba(0,95,235,.25);
 <p class="myBox">This whole box should link.<a href="#" class="cover"></a></p>

You also have the option of putting the anchor outside whatever you want to link (based on your edit, I see this may not be an option?):您还可以选择将锚点放在要链接的任何内容之外(根据您的编辑,我认为这可能不是一个选项?):

 .myBox{padding:10px;border:1px solid #ccc;position: relative;}
 <a href="#"><p class="myBox">This whole box should link.</p>

If you DO need jQuery for some reason, you can handle all of your jQuery functions in a single ready function, and pass the click event handler to it.如果出于某种原因确实需要 jQuery,则可以在一个ready函数中处理所有 jQuery 函数,并将单击事件处理程序传递给它。 (Note, you should also handle the situation where a link is NOT present, otherwise you'll send users to a blank/broken URL if its clicked on): (注意,您还应该处理链接不存在的情况,否则如果点击它,您会将用户发送到一个空白/损坏的 URL):

 jQuery(document).ready(function($){ $('.myBox').on('click', function(){ var location = $(this).find('a').attr('href'); if( location ) window.location = location; }); });
 .myBox{ padding: 10px; border: 10px; } .myBox:hover {cursor: pointer; background: #0095ee;}
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="myBox"> <p>This is test.<a href="https://example.com/">This is a link</a></p> </div> <div class="myBox"> <p>This is with no link</p> </div>

jQuery(".myBox").click(function($) {
    window.location = $(this).find("a").attr("href");
    return false;
});

Your issue is that you have $ on the parameters of the click event handler.您的问题是您在点击事件处理程序的参数上有$ That variable passed in is the Event generated, not jQuery .传入的变量是生成的Event ,而不是jQuery There are a few ways to fix this.有几种方法可以解决这个问题。

1. Using the global variable 1. 使用全局变量

jQuery(".myBox").click(function() {
    window.location = jQuery(this).find("a").attr("href");
    return false;
});

2. Storing jQuery in $ 2. 在 $ 中存储 jQuery

var $ = jQuery;

$(".myBox").click(function() {
    window.location = $(this).find("a").attr("href");
    return false;
});

3. Using a document ready that passes jQuery in, allowing you to name it $ 3. 使用传入 jQuery 的就绪文档,允许您将其命名为 $

jQuery(document).ready(function($){
  $(".myBox").click(function() {
    window.location = $(this).find("a").attr("href");
    return false;
  });
});

4. Using a simple IIFE. 4. 使用简单的 IIFE。 Similar to a document ready, but less overhead类似于准备好文档,但开销更少

(function($){
  $(".myBox").click(function() {
    window.location = $(this).find("a").attr("href");
    return false;
  });
}(jQuery));

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

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