如何获取多个属性值?
我可以正确地获得href
,我也希望获得alt
,但我无法正确判断并获得正确的结果。
我尝试了.attr('href').attr('alt')
.attr('href' + 'alt')
以及.attr({href, alt})
,
以下是在线示例: http://jsfiddle.net/Lm4TM/
JS:
$(document).ready(function(){
$(".link").each(function (i) {
var links = $(this).find("a");
$(this).wrapInner("<a href='"+ $(links).attr('href') + "'></a>");
});
});
CSS:
.link {
width:20%;
height: 100%;
background: red;
float: left;
margin: 0 20px;
}
a {
text-decoration: none;
color: white;
}
HTML:
<div class="link">
<a href="http://www.google.com" alt="google">Google.com</a>
<p> Google Inc. is an American multinational corporation that provides Internet-related products and services, including internet search, cloud computing, software and advertising technologies.Advertising revenues from AdWords generate almost all of the company's profits</p>
</div>
<div class="link">
<a href="http://www.microsoft.com" alt="microsoft">Microsoft.com</a>
<p>Microsoft Corporation is an American multinational software corporation headquartered in Redmond, Washington that develops, manufactures, licenses, and supports a wide range of products and services related to computing.</p>
</div>
<div class="link">
<a href="http://www.Apple.com" alt="Apple">Microsoft.com</a>
<p>The apple is the pomaceous fruit of the apple tree, species Malus domestica in the rose family (Rosaceae). It is one of the most widely cultivated tree fruits, and the most widely known of the many members of genus Malus that are used by humans.</p>
</div>
答案 0 :(得分:2)
您是否尝试过这样做?
$(this).wrapInner("<a href='"+ $(links).attr('href') + "' alt='"+$(links).attr('alt')+"'></a>");
我更新了你的小提琴。 http://jsfiddle.net/Lm4TM/1/
出于好奇,你想要完成什么?