2014-04-07 26 views
0

我正在使用jQuery來使用$ .each函數更改所有定位標記的href屬性。 我可以在控制檯中獲取新的href,但點擊元素後,它不會重定向到已更改的新網址。無法使用jquery將新url加載到href屬性

$(".section-icons").each(function() { 
    var newHref = $(this).find("a").attr("href"); 
    newHref += "?currentMod=sales_target"; 
    console.log(newHref); 
    $(this).find("a").attr("href", newHref); 
}); 

<div class="section-icons"> 
    <a href="sales-target-revenue-wise.php"> 
     <div class="modules-icons-dd"> 
      <img src="<?=$dir_path?>images/module-icons/sales-target-revenue-wise-icon.png" /> 
     </div> 
     <div class="section-text">Revenue Wise Sales Target</div> 
    </a> 
</div> 

回答

2

這樣做:

$(document).ready(function() { 
    $(".section-icons").each(function() { 
     var newHref = $(this).find("a").attr("href"); 
     newHref += "?currentMod=sales_target"; 
     console.log(newHref); 
     $(this).find("a").attr("href", newHref); 
    }); 
}); 

你需要做的是後DOM已準備就緒。

而且,你的腳本需要內部<script></script>

Demo

+0

Downvoter被包裹,小心地闡述? –

+0

你怎麼能說OP沒有在DOM中包裝代碼? –

+1

我可以這樣說,因爲他沒有把它包裹在問題中。爲什麼任何一個正確思想的人都會剝離他們的代碼並在SO上只粘貼部分代碼? –

相關問題