2013-03-10 62 views
0

如何获取多个属性值? 我可以正确地得到href,我也想得到alt,但我无法弄清楚,并得到正确的结果。 我试图.attr('href').attr('alt').attr('href' + 'alt').attr({href, alt})jQuery在wrapInner内部更改多个attr

这里是在线样本: 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> 
+2

不需要将'链接'包装在jquery对象中。它已经是一个了。 – AlienWebguy 2013-03-10 23:00:28

+0

@AlienWebguy谢谢 – olo 2013-03-10 23:41:08

回答

2

你试过只是在做什么?

$(this).wrapInner("<a href='"+ $(links).attr('href') + "' alt='"+$(links).attr('alt')+"'></a>"); 

我更新了你的小提琴。 http://jsfiddle.net/Lm4TM/1/

出于好奇,你想完成什么?

+0

非常感谢您的回答!我想将该块作为“LINK” – olo 2013-03-10 23:42:11

+1

感谢您的澄清。 – JuanT 2013-03-10 23:46:56