2016-02-12 47 views
-1

我有這個代碼與跨度替換項目的Jquery

$('.main_image a').each(function() { 
 
\t $(this+':last-child').replaceWith($('<span></span>')); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="main_image"> 
 
    <a href="#"></a> 
 
    <a href="#"></a> 
 
    <a href="#"></a> 
 
    <a href="##"></a> 
 
    <a href="#"></a> 
 
    <a href="#"></a> 
 
</div> 
 
<div class="main_image"> 
 
    <a href="#"></a> 
 
    <a href="#"></a> 
 
    <a href="#"></a> 
 
    <a href="##"></a> 
 
    <a href="#"></a> 
 
    <a href="#"></a> 
 
</div>

我想每個.main_image最後a替換成span。我嘗試在Jquery中完成,但它不起作用。

感謝您的幫助!

回答

4

您不需要使用每種方法。只需選擇目標元素並替換它們:

$('.main_image a:last-child')//selects each last a tag of main_image 
    .replaceWith($('<span></span>'));//then replace with 
+0

它的工作非常感謝! –

+0

很高興幫助... –