【问题标题】:Copy href from <a> inside a DIV with unique id and paste somewhere else in that DIV从 <a> 复制具有唯一 ID 的 DIV 中的 href 并粘贴到该 DIV 中的其他位置
【发布时间】:2019-11-18 20:08:03
【问题描述】:

我正在尝试找到一种方法从data-name="entity_field_post_title" div 复制href,并将其用作data-type="image" div 的href

这样做的目的是打开页面的相应链接,而不是打开它的照片。这些是帖子的缩影,我想自己打开帖子,而不是照片。

重要的是要指出顶部 div 的 ID 编号每次都会发生不可预测的变化。

<div id="drts-content-post-1163">
    <div class="drts-display-element-columns-3">
        <div class="drts-row drts-gutter-none">
            <div class="slick-list draggable">
                <div class="slick-track">
                    <div data-type="image" class="slick-slide">
                        <a href="http://localhost/wp/wp-content/uploads/horse.jpeg" ><figure><img src="http://localhost/wp/wp-content/uploads/horse.jpeg"></figure></a>
                    </div>
                    <div data-type="image" class="slick-slide">
                        <a href="http://localhost/wp/wp-content/uploads/panda.png" ><figure><img src="http://localhost/wp/wp-content/uploads/panda.png"></figure></a>
                    </div>
                </div>          
            </div>
        </div>
        <div data-name="column" class="drts-display-element-column-6">
            <div class="drts-row drts-gutter-none">
                <div data-name="entity_field_post_title" class="directory-listing-title ">
                    <a href="http://localhost/wp/anuncios/prueba-desde-backend-1/" target="_self" class="drts-entity-1163">TEXT</a>
                </div>
            </div>
        </div>
    </div>
</div>

【问题讨论】:

    标签: jquery href


    【解决方案1】:

    我将选择器保留为变量,以防您需要对它们进行细微更改,但基础工作 - 循环遍历每个 containerDiv,从 linkDiv &gt; &lt;a&gt; 获取 href,并将该链接应用于每个 @ 987654323@

    let containerDiv = 'div[id*="drts-content-post-"]';
    let linkDiv = 'div[data-name="entity_field_post_title"]';
    let imageDiv = 'div[data-type="image"]';
    
    let $container = $(containerDiv).has(linkDiv);
    
    $container.each((index, element) => {
      let link = $(element).find(`${linkDiv} > a`).attr('href');
      $(element).find(`${imageDiv} > a`).attr('href', link);
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="drts-content-post-1163">
        <div class="drts-display-element-columns-3">
            <div class="drts-row drts-gutter-none">
                <div class="slick-list draggable">
                    <div class="slick-track">
                        <div data-type="image" class="slick-slide">
                            <a href="http://localhost/wp/wp-content/uploads/horse.jpeg" ><figure><img src="http://localhost/wp/wp-content/uploads/horse.jpeg"></figure></a>
                        </div>
                        <div data-type="image" class="slick-slide">
                            <a href="http://localhost/wp/wp-content/uploads/panda.png" ><figure><img src="http://localhost/wp/wp-content/uploads/panda.png"></figure></a>
                        </div>
                    </div>          
                </div>
            </div>
            <div data-name="column" class="drts-display-element-column-6">
                <div class="drts-row drts-gutter-none">
                    <div data-name="entity_field_post_title" class="directory-listing-title ">
                        <a href="http://localhost/wp/anuncios/prueba-desde-backend-1/" target="_self" class="drts-entity-1163">TEXT</a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    【讨论】:

    • 这取决于代码中的结构 - 但是可以链接 .has() 方法,因此如果可以的话,您可以添加对类/ID 的额外检查。否则,发布附加代码会很有帮助,以便我们更容易看到可以做什么
    • 错误信息?您看到 HTML 有什么变化吗?
    • 我不知道,我发布的 sn-p 似乎按预期工作,所以我认为问题在于对完整代码的调整;如果没有看到该代码,我就无能为力了
    • 干杯;等我看了一下我会编辑我的帖子
    • 可能值得在我的代码中获取一些控制台日志 - console.log($(element).find(${imageDiv} > a).attr('href')) 作为每个的最后一行应该确保它运行
    【解决方案2】:

    我不确定我是否理解,但如果您只想将图片制作成链接,您可以使用带有背景图片的锚标签,如下所示:

    <a class="photo" href="website.net/link" title="photo" id="photo">photo</a>
    

    【讨论】:

    • 没有@Gandalfion。让我再次尝试解释一下自己。我需要替换 class="div1" 的当前 href 以“#1163 .div1 a href = #1163 .div-good-one a href ”和“#734 .div1 a href = #734 .div-good -one a href " 并对页面中的每个其他#id 重复此操作(有很多)。
    • 对于每个 div 容器,您希望图像 a 标签的 href 与 .div-good-one 的 href 相同吗?
    • 没错! (对于每个具有不同 id 的容器)
    【解决方案3】:

    感谢 Light 找到了解决方案。 第一部分是检测 JS 和 jQuery 是否在 Wordpress 中运行。

    // Select all the top level divs, without using ID
    
    alert("JS works");
    if (jQuery) {  
      alert('Jquery Loaded');
    } else {
      alert('Jquery Not Loaded');
    }
    

    这就是 jQuery 在 Wordpress 中的设置方式,因为 $ 否则设置为函数。我创建了“linko”来了解.find() 内部发生的事情

    // jQuery code:
    jQuery( function( $ ){
    
      // Safely use the $ alias here
      // Code here will be executed on the document.ready event
      let containerDiv = 'div[id*="drts-content-post-"]';
      let linkDiv = 'div[data-name="entity_field_post_title"]';
      let imageDiv = 'div[data-type="image"]';
    
      let $container = $(containerDiv).has(linkDiv);
    
      $container.each((index, element) => {
        let link = $(element).find(`div[data-name="entity_field_post_title"] > a`).attr('href');
        console.log(link);
        let linko = $(element).find(`div[data-type="image"] > a`).attr('href');
        console.log(linko);
    
        $(element).find(`div[data-type="image"] > a`).attr('href', link);
        console.log($(element).find(`div[data-type="image"] > a`).attr('href'));
      })
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多