如何使用 jQuery 将属性值复制到兄弟属性
How to copy an a attribut value to an a sibling attribut using jQuery
我有一个 ul
列表元素,其中每个 li
元素中有一个 div
,里面有 2 个 children div's
,我想要它的第二个div > a
元素与 hover_icon_link
的 class 具有与第一个 div > a
元素相同的 href attr
,我的代码仅适用于第一个 li
元素,其余的都在每一里的任何地方都采用相同的 attr,我的代码如下
jQuery
:
$('li.product-category').each(function(){
var attrLink = jQuery('div.post_featured > a').attr("href");
$('div.post_thumb > a.hover_icon_link').attr("href", attrLink);
});
HTML:
<ul class="products">
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link1">Title here</a>
</div>
<div class="post_thumb">
<a href="#link2" class="hover_icon_link">title here</a>
</div>
</div>
</li>
</ul>
我解决了我的问题,这是代码:
$('li.product-category').each(function(){
var attrLink = $(this).children("a").attr("href");
$(this).find("a.hover_icon_link").attr("href", attrLink);
});
虽然你已经 self-answered 你的问题,这表明你已经自己解决了这个问题,我想我会贡献另一个答案来展示一个替代方案,以及对两个原始问题的解释你所面临的问题和解决方案。
那么,首先,您遇到的问题是在您的代码中:
// you select each <li> element with the class of '.product-category',
// and use the each() method to iterate over that collection:
$('li.product-category').each(function(){
// here you select all elements that match the selector, meaning
// you retrieve <a> every element on the page which is a child
// of a <div class="post_featured"> element and retrieve the
// 'href' attribute-value with the attr() method. When you use
// a jQuery getter method on a collection the value returned is
// the attribute-value of the first element in that collection
// (of every element on the page that matches) not necessarily
// the attribute-value of the intended <a>:
var attrLink = jQuery('div.post_featured > a').attr("href");
// here you select every <a class="hover_icon_link"> element in
// the document that is a child of a <div class="post_thumb">
// element, and use the attr() method as a setter to update the
// href attribute of every element in the document to the value
// retrieved earlier:
$('div.post_thumb > a.hover_icon_link').attr("href", attrLink);
});
// here we select all elements with the class of 'post_item_wrap'
// and iterate over them using the each() method:
$('.post_item_wrap').each(function() {
// here we search within the current .post_item_wrap element,
// represented by $(this), and within the current element we
// use the find() method to find an <a> element which is a
// descendant of an element with the class of 'post_featured',
// and retrieve that href attribute-value:
let href = $(this).find('.post_featured a').attr('href');
// we again search within the current .post_item_wrap element
// to find an <a> element which is a descendant of an element
// with a class of 'post_thumb', and then update its href
// attribute-value with the value find previously:
$(this).find('.post_thumb a').attr('href', href);
});
*,
::before,
::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
ul,
li {
list-style-type: none;
}
li {
border: 2px solid rebeccapurple;
border-radius: 0.5em;
overflow: hidden;
margin: 0.5em auto;
}
div.post_featured {
background-color: lightblue;
margin: 0.3em 0;
}
div.post_thumb {
background-color: palegreen;
}
a {
display: flex;
justify-content: space-around;
align-content: center;
width: 40vh;
}
a::after {
display: content;
content: '(' attr(href) ')';
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="products">
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link1">Title here</a>
</div>
<div class="post_thumb">
<a href="#link2" class="hover_icon_link">title here</a>
</div>
</div>
</li>
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link3">Title here</a>
</div>
<div class="post_thumb">
<a href="#link4" class="hover_icon_link">title here</a>
</div>
</div>
</li>
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link5">Title here</a>
</div>
<div class="post_thumb">
<a href="#link6" class="hover_icon_link">title here</a>
</div>
</div>
</li>
</ul>
当然,这可以通过解构来改善,例如:
// here we select all elements with the class of 'post_item_wrap',
// and then iterate over each of those elements using the each()
// method:
$('.post_item_wrap').each(function(index, element) {
// here we take advantage of the 'element', made available via jQuery's
// anonymous function, and use Element.querySelectorAll() to retrieve both
// <a> elements within the current .post_item_wrap element; we use
// JavaScript's destructuring to assign those <a> elements to the variables
// 'a' and 'b' (these are assigned in the order in which the <a> elements
// are placed in the DOM):
let [a, b] = element.querySelectorAll('a');
// here we use Element.setAttribute() to update the attribute-value of the
// 'href' attribute of the second <a> to be equal to the attribute-value
// of the 'href' of the first <a> element:
b.setAttribute('href', a.getAttribute('href'));
// We could instead use:
// b.href = a.href;
// but that will use an absolute URL which may not be what you want:
});
*,
::before,
::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
ul,
li {
list-style-type: none;
}
li {
border: 2px solid rebeccapurple;
border-radius: 0.5em;
overflow: hidden;
margin: 0.5em auto;
width: 15em;
}
div.post_featured {
background-color: lightblue;
margin: 0.3em 0;
}
div.post_thumb {
background-color: palegreen;
}
a {
display: flex;
justify-content: space-around;
align-content: center;
}
a::after {
display: content;
content: '(' attr(href) ')';
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="products">
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link1">Title here</a>
</div>
<div class="post_thumb">
<a href="#link2" class="hover_icon_link">title here</a>
</div>
</div>
</li>
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link3">Title here</a>
</div>
<div class="post_thumb">
<a href="#link4" class="hover_icon_link">title here</a>
</div>
</div>
</li>
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link5">Title here</a>
</div>
<div class="post_thumb">
<a href="#link6" class="hover_icon_link">title here</a>
</div>
</div>
</li>
</ul>
当然,因为也可以在普通 JavaScript 中迭代 element-nodes 的节点列表,所以没有什么能阻止我们在普通 JavaScript 中编写以前的方法,给出:
// here we use document.querySelectorAll() to retrieve all elements in the
// document that match the selector '.post_item_wrap', and then chain that
// with NodeList.prototype.forEach() to iterate over that NodeList:
document.querySelectorAll('.post_item_wrap').forEach(
// using an Arrow function (since we have no need of 'this', and have
// a reference to the current Node of the NodeList, which we pass into
// the function:
(element) => {
// as above, we use Element.querySelectorAll() to retrieve all <a>
// elements found within the current element-node, and use
// destructuring to assign the first two of those <a> elements to
// the variables 'a' and 'b':
let [a, b] = element.querySelectorAll('a');
// exactly as above:
b.setAttribute('href', a.getAttribute('href'));
});
*,
::before,
::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
ul,
li {
list-style-type: none;
}
li {
border: 2px solid rebeccapurple;
border-radius: 0.5em;
overflow: hidden;
margin: 0.5em auto;
width: 15em;
}
div.post_featured {
background-color: lightblue;
margin: 0.3em 0;
}
div.post_thumb {
background-color: palegreen;
}
a {
display: flex;
justify-content: space-around;
align-content: center;
}
a::after {
display: content;
content: '(' attr(href) ')';
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="products">
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link1">Title here</a>
</div>
<div class="post_thumb">
<a href="#link2" class="hover_icon_link">title here</a>
</div>
</div>
</li>
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link3">Title here</a>
</div>
<div class="post_thumb">
<a href="#link4" class="hover_icon_link">title here</a>
</div>
</div>
</li>
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link5">Title here</a>
</div>
<div class="post_thumb">
<a href="#link6" class="hover_icon_link">title here</a>
</div>
</div>
</li>
</ul>
参考文献:
我有一个 ul
列表元素,其中每个 li
元素中有一个 div
,里面有 2 个 children div's
,我想要它的第二个div > a
元素与 hover_icon_link
的 class 具有与第一个 div > a
元素相同的 href attr
,我的代码仅适用于第一个 li
元素,其余的都在每一里的任何地方都采用相同的 attr,我的代码如下
jQuery
:
$('li.product-category').each(function(){
var attrLink = jQuery('div.post_featured > a').attr("href");
$('div.post_thumb > a.hover_icon_link').attr("href", attrLink);
});
HTML:
<ul class="products">
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link1">Title here</a>
</div>
<div class="post_thumb">
<a href="#link2" class="hover_icon_link">title here</a>
</div>
</div>
</li>
</ul>
我解决了我的问题,这是代码:
$('li.product-category').each(function(){
var attrLink = $(this).children("a").attr("href");
$(this).find("a.hover_icon_link").attr("href", attrLink);
});
虽然你已经 self-answered 你的问题,这表明你已经自己解决了这个问题,我想我会贡献另一个答案来展示一个替代方案,以及对两个原始问题的解释你所面临的问题和解决方案。
那么,首先,您遇到的问题是在您的代码中:
// you select each <li> element with the class of '.product-category',
// and use the each() method to iterate over that collection:
$('li.product-category').each(function(){
// here you select all elements that match the selector, meaning
// you retrieve <a> every element on the page which is a child
// of a <div class="post_featured"> element and retrieve the
// 'href' attribute-value with the attr() method. When you use
// a jQuery getter method on a collection the value returned is
// the attribute-value of the first element in that collection
// (of every element on the page that matches) not necessarily
// the attribute-value of the intended <a>:
var attrLink = jQuery('div.post_featured > a').attr("href");
// here you select every <a class="hover_icon_link"> element in
// the document that is a child of a <div class="post_thumb">
// element, and use the attr() method as a setter to update the
// href attribute of every element in the document to the value
// retrieved earlier:
$('div.post_thumb > a.hover_icon_link').attr("href", attrLink);
});
// here we select all elements with the class of 'post_item_wrap'
// and iterate over them using the each() method:
$('.post_item_wrap').each(function() {
// here we search within the current .post_item_wrap element,
// represented by $(this), and within the current element we
// use the find() method to find an <a> element which is a
// descendant of an element with the class of 'post_featured',
// and retrieve that href attribute-value:
let href = $(this).find('.post_featured a').attr('href');
// we again search within the current .post_item_wrap element
// to find an <a> element which is a descendant of an element
// with a class of 'post_thumb', and then update its href
// attribute-value with the value find previously:
$(this).find('.post_thumb a').attr('href', href);
});
*,
::before,
::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
ul,
li {
list-style-type: none;
}
li {
border: 2px solid rebeccapurple;
border-radius: 0.5em;
overflow: hidden;
margin: 0.5em auto;
}
div.post_featured {
background-color: lightblue;
margin: 0.3em 0;
}
div.post_thumb {
background-color: palegreen;
}
a {
display: flex;
justify-content: space-around;
align-content: center;
width: 40vh;
}
a::after {
display: content;
content: '(' attr(href) ')';
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="products">
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link1">Title here</a>
</div>
<div class="post_thumb">
<a href="#link2" class="hover_icon_link">title here</a>
</div>
</div>
</li>
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link3">Title here</a>
</div>
<div class="post_thumb">
<a href="#link4" class="hover_icon_link">title here</a>
</div>
</div>
</li>
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link5">Title here</a>
</div>
<div class="post_thumb">
<a href="#link6" class="hover_icon_link">title here</a>
</div>
</div>
</li>
</ul>
当然,这可以通过解构来改善,例如:
// here we select all elements with the class of 'post_item_wrap',
// and then iterate over each of those elements using the each()
// method:
$('.post_item_wrap').each(function(index, element) {
// here we take advantage of the 'element', made available via jQuery's
// anonymous function, and use Element.querySelectorAll() to retrieve both
// <a> elements within the current .post_item_wrap element; we use
// JavaScript's destructuring to assign those <a> elements to the variables
// 'a' and 'b' (these are assigned in the order in which the <a> elements
// are placed in the DOM):
let [a, b] = element.querySelectorAll('a');
// here we use Element.setAttribute() to update the attribute-value of the
// 'href' attribute of the second <a> to be equal to the attribute-value
// of the 'href' of the first <a> element:
b.setAttribute('href', a.getAttribute('href'));
// We could instead use:
// b.href = a.href;
// but that will use an absolute URL which may not be what you want:
});
*,
::before,
::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
ul,
li {
list-style-type: none;
}
li {
border: 2px solid rebeccapurple;
border-radius: 0.5em;
overflow: hidden;
margin: 0.5em auto;
width: 15em;
}
div.post_featured {
background-color: lightblue;
margin: 0.3em 0;
}
div.post_thumb {
background-color: palegreen;
}
a {
display: flex;
justify-content: space-around;
align-content: center;
}
a::after {
display: content;
content: '(' attr(href) ')';
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="products">
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link1">Title here</a>
</div>
<div class="post_thumb">
<a href="#link2" class="hover_icon_link">title here</a>
</div>
</div>
</li>
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link3">Title here</a>
</div>
<div class="post_thumb">
<a href="#link4" class="hover_icon_link">title here</a>
</div>
</div>
</li>
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link5">Title here</a>
</div>
<div class="post_thumb">
<a href="#link6" class="hover_icon_link">title here</a>
</div>
</div>
</li>
</ul>
当然,因为也可以在普通 JavaScript 中迭代 element-nodes 的节点列表,所以没有什么能阻止我们在普通 JavaScript 中编写以前的方法,给出:
// here we use document.querySelectorAll() to retrieve all elements in the
// document that match the selector '.post_item_wrap', and then chain that
// with NodeList.prototype.forEach() to iterate over that NodeList:
document.querySelectorAll('.post_item_wrap').forEach(
// using an Arrow function (since we have no need of 'this', and have
// a reference to the current Node of the NodeList, which we pass into
// the function:
(element) => {
// as above, we use Element.querySelectorAll() to retrieve all <a>
// elements found within the current element-node, and use
// destructuring to assign the first two of those <a> elements to
// the variables 'a' and 'b':
let [a, b] = element.querySelectorAll('a');
// exactly as above:
b.setAttribute('href', a.getAttribute('href'));
});
*,
::before,
::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
ul,
li {
list-style-type: none;
}
li {
border: 2px solid rebeccapurple;
border-radius: 0.5em;
overflow: hidden;
margin: 0.5em auto;
width: 15em;
}
div.post_featured {
background-color: lightblue;
margin: 0.3em 0;
}
div.post_thumb {
background-color: palegreen;
}
a {
display: flex;
justify-content: space-around;
align-content: center;
}
a::after {
display: content;
content: '(' attr(href) ')';
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="products">
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link1">Title here</a>
</div>
<div class="post_thumb">
<a href="#link2" class="hover_icon_link">title here</a>
</div>
</div>
</li>
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link3">Title here</a>
</div>
<div class="post_thumb">
<a href="#link4" class="hover_icon_link">title here</a>
</div>
</div>
</li>
<li class="product-category">
<div class="post_item_wrap">
<div class="post_featured">
<a href="#link5">Title here</a>
</div>
<div class="post_thumb">
<a href="#link6" class="hover_icon_link">title here</a>
</div>
</div>
</li>
</ul>
参考文献: