Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在DOM树中使用Jquery移动属性_Javascript_Jquery_Html - Fatal编程技术网

Javascript 在DOM树中使用Jquery移动属性

Javascript 在DOM树中使用Jquery移动属性,javascript,jquery,html,Javascript,Jquery,Html,我正在尝试获取一个元素的href属性,它位于树的相当深处。 这就是我目前正在调整的代码 $('.widget-pSidebar > ul > li > article > section > div > article > a').each(function () { var path = $(this).parent().find('footer').children().children().attr('href');

我正在尝试获取一个元素的href属性,它位于树的相当深处。 这就是我目前正在调整的代码

$('.widget-pSidebar > ul > li > article > section > div > article > a').each(function () {
        var path = $(this).parent().find('footer').children().children().attr('href');
        alert(path);
        $(this).attr('href', path);
    });
而这就是那棵树

<a>
    <img>
</a>
<footer>
    <h1>
        <a>
    </h1>

我想从最后一个中提取href并将其放入第一个中


编辑:我重新编写了代码,可以访问,但不幸的是我一次又一次地复制同一链接。

为了方便起见,在每个链接上添加一个id

<a id="link-to">
    <img>
</a>
<footer>
    <h1>
        <a id="link-from">
    </h1>
完成了

--编辑-- 如果我们有一份清单或类似的东西

<div class='list-item'>
    <a class="link-to">
        <img>
    </a>
    <footer>
        <h1>
            <a class="link-from">
        </h1>

完成:)

您可以在html中添加一些类吗

然后你可以这样写:

 <a class="destiny">
   <img>
 </a>
 <footer>
   <h1>
     <a class="source" href="href1"></a>
   </h1>
</footer>
$('.destiny').attr('href',$('.source').attr('href'));
$('.foo').each(function() {
    var path = $(this).next().children().first().children().first().attr('href');
    $(this).attr('href', path);
});
工作示例:


您必须检查小提琴的结果。

给他们一个标识符,如

<a class="anchor1">
    <img>
</a>
<footer>
    <h1>
        <a>
    </h1>
</footer>
}))

类似这样:

 <a class="destiny">
   <img>
 </a>
 <footer>
   <h1>
     <a class="source" href="href1"></a>
   </h1>
</footer>
$('.destiny').attr('href',$('.source').attr('href'));
$('.foo').each(function() {
    var path = $(this).next().children().first().children().first().attr('href');
    $(this).attr('href', path);
});

请您向我们展示您的整个HTML结构。只发布所有内容的部分就足够了吗?从您展示的摘录来看,您的代码似乎应该可以工作。你能做一个JSFIDLE来演示这个问题吗?@Rentonie最好有几个部分。他可能有多次重复,所以它需要类,而不是ID。我有一个列表,我想编辑其中的每一个,所以我确定如果这样做,我会使用每个不同的链接。他有很多这样的东西,这就是为什么他使用
.each()
。你的答案会将所有的标记更新为相同的值。他在标记上有多个标记?然后他应该重新考虑他的html的结构……这是orchard的结构。我只是指出我想要列表项的“页脚”中的X元素。我真的没有得到你需要的。您希望读取页脚上的锚点的href,并将它们分配给带有“正文”(而不是页脚)上图像的锚点。这就是你需要的吗?