简体   繁体   English

如何在div标签周围包裹标签?

[英]How do I wrap an a tag around a div tag?

I'm modifying a WordPress theme and I have this piece of code: 我正在修改WordPress主题,并且具有以下代码:

// Product link
$product_link_atts = ' href="' . esc_url( get_permalink() ) . '"';

<a<?php echo $product_link_atts; ?>><!-- Begin my a tag -->
    <div class="nm-shop-loop-details">
        <?php if ( $nm_globals['wishlist_enabled'] ) : ?>
        <div class="nm-shop-loop-wishlist-button"><?php nm_wishlist_button(); ?></div>
        <?php endif; ?>

        <h3><a<?php echo $product_link_atts; ?>><?php the_title(); ?></a></h3>
        <?php 
            /**
             * Hook: woocommerce_shop_loop_item_title.
             */
            do_action( 'woocommerce_shop_loop_item_title' );
        ?>

        <div class="nm-shop-loop-after-title <?php echo esc_attr( $nm_theme_options['product_action_link'] ); ?>">
            <div class="nm-shop-loop-price">
                <?php
                    /**
                     * Hook: woocommerce_after_shop_loop_item_title.
                     *
                     * @hooked woocommerce_template_loop_price - 10
                     */
                    do_action( 'woocommerce_after_shop_loop_item_title' );
                ?>
            </div>

            <div class="nm-shop-loop-actions">
                <?php
                    if ( $quickview_enabled ) {
                        echo apply_filters( 'nm_product_quickview_link', '<a' . $quickview_link_atts . '>' . esc_html__( 'Show more', 'nm-framework' ) . '</a>' );
                    }

                    /**
                     * Hook: woocommerce_after_shop_loop_item.
                     *
                     * @hooked woocommerce_template_loop_add_to_cart - 10
                     */
                    do_action( 'woocommerce_after_shop_loop_item' );
                ?>
            </div>
        </div>
    </div>
</a><!-- Close my a tag -->

I'm trying to wrap an a tag around this piece of code (starts line 4). 我正在尝试在这段代码周围包装一个标签(从第4行开始)。

<a<?php echo $product_link_atts; ?>><!-- Begin my a tag -->

But somehow the a tag is broken when it's compiled. 但是以某种方式编译标签时,标签会损坏。

This is how it looks like when inspected via Chrome: 通过Chrome检查时的外观如下: 问题

As you can see the a element is being closed before it wraps itself around the div. 如您所见,a元素在将其自身环绕div之前已关闭。 I've done some research and I read that since HTML5 it's possible to wrap a elements around div elements. 我已经做过一些研究,并且我读到,自HTML5以来,可以将元素包装在div元素周围。

I'm not really sure what I'm missing here, am I just missing something stupid, or am I actually trying to do something that doesn't work. 我不太确定自己在这里缺少什么,是只是错过了一些愚蠢的事情,还是我实际上是在尝试做一些不起作用的事情。

HTML forbids nesting anchor tags inside other anchor tags. HTML禁止在其他锚标记内嵌套锚标记。

This is why you won't be able to do it and this is why it closes itself before it happens. 这就是为什么您将无法执行此操作的原因,这就是为什么它在发生之前会自行关闭的原因。

Why are nested anchor tags illegal? 为什么嵌套的锚标签是非法的?

We need to see the output of some of the php values there to get a better idea of what html really looks like. 我们需要查看一些php值的输出,以更好地了解html的实际外观。 If you could take a screenshot of the sourcecode (ctrl+U) it would be more helpful than what was interpreted by chrome. 如果您可以截取源代码(ctrl + U)的屏幕快照,它将比chrome解释的要有用。

However, I've noticed you have an anchor element inside another one. 但是,我注意到您在另一个元素内有一个锚元素。

Avoid doing this. 避免这样做。

https://www.w3.org/TR/2011/WD-html5-20110525/text-level-semantics.html#the-a-element https://www.w3.org/TR/2011/WD-html5-20110525/text-level-semantics.html#the-a-element

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

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