简体   繁体   English

制作一个 <div> 在Wordpress中可点击

[英]Making a <div> Clickable in Wordpress

I'm working on modifying an existing wordpress portfolio page. 我正在修改现有的wordpress产品组合页面。 All that is left to do is to make it so when the user clicks anywhere in the article list the link will open. 剩下要做的就是做到这一点,因此,当用户单击文章列表中的任意位置时,链接将打开。 Currently it is just when the title or image is clicked. 当前只是单击标题或图像。

I can see that I could make a clickable with the following setup: 我可以看到可以使用以下设置进行点击:

    <a href="http://example.com">
     <div>
     anything
     </div>
    </a>

However Wordpress moves the tags when the page loads like so: 但是,Wordpress会在页面加载时像下面那样移动标签:

<a href="http://example.com">
</a>
<div>
 anything
</div>

So I started to work on using css with the following setup: 因此,我开始通过以下设置使用CSS:

HTML HTML

<div class= "box">
</div>

CSS CSS

div.box {
    position: relative;
}

div.box:hover {
    cursor: hand;
    cursor: pointer;
    opacity: .9;
}

The hover works, it triggers over all of the content and a cursor does appear. 悬停起作用,它会触发所有内容,并且会出现一个光标。

However thats as much as I can do so far. 但是,那是我到目前为止所能做的。 Any attempt to interact with the .box is shot down. 与.box进行交互的任何尝试都会被击落。 Nothing will trigger with javascript using the following : 使用以下代码,JavaScript不会触发任何操作:

$(".box").click(function() {
   alert("I am an alert box!"); 
});

I cannot seem to interact with the div. 我似乎无法与div互动。

The page is as follows: 页面如下:

<div class= "box">

<article id="post-<?php the_ID(); ?>" class="portfolio-article clearfix">

    <?php if( get_post_meta($post->ID, "portfolio_image", true) ): ?> 
        <a href="<?php the_field('portfolio_link'); ?>" target="_blank"><img src="<?php the_field('portfolio_image'); ?>" class="portfolio-image"></a>
        <!--get PDF if not empty-->
        <?php else: ?>
        <img src="http://domain.com/wp-content/uploads/2014/02/placeholder.png" class="portfolio-image">
    <?php endif; ?>

    <div class="portfolio-content">
        <header>
            <?php  if( get_post_meta($post->ID, "portfolio_link", true) ): ?> 
                <h1 class="portfolio-title">
                    <a target="_blank" href="<?php the_field('portfolio_link'); ?>">
                        <?php the_field('portfolio_title'); ?> <span class="sosa-icon">p</span>
                    </a>
                </h1>
                <!--get PDF if not empty-->
            <?php else: ?>
                <h1 class="portfolio-title"><?php the_field('portfolio_title'); ?></h1>
            <?php endif; ?>
        </header>

        <p class="portfolio-meta">
            <?php the_field('portfolio_publication_and_date'); ?> 
            &nbsp;
            <?php if( get_post_meta($post->ID, "portfolio_pdf_upload", true) ): ?> 
                <a href="<?php the_field('portfolio_pdf_upload'); ?>" target="_blank"><span class="sosa-icon">H</span> Open Pdf</a><!--get PDF if not empty-->
            <?php endif; ?>
        </p><!-- END ortfolio-meta -->
       </div><!--portfolio-content-->


</article>

</div>

NewToJS was in the right area when saying it could be a problem with the access to the jQuery library. 说要访问jQuery库可能存在问题时,NewToJS处于正确的位置。 In this case I had linked jQuery correctly, it was tested and working. 在这种情况下,我已经正确链接了jQuery,它已经过测试并且可以正常工作。

jQuery in Wordpress needs to be enqueued by adding the following into functions.php 需要通过将以下内容添加到functions.php中来使Wordpress中的jQuery入队

wp_enqueue_script("jquery");

My fault was because I didn't change the '$' to 'jQuery' as this is necessary when working with Wordpress. 我的错是因为我没有将'$'更改为'jQuery',因为在使用Wordpress时这是必需的。

Here is the incorrect usage : 这是不正确的用法:

$(".box").click(function() {
   alert("I am an alert box!"); 
});

Here is the correct usage : 这是正确的用法:

jQuery(".box").click(function() {
   alert("I am an alert box!"); 
});

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

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