我想基於URL的內容追加到無序列表中。因此,如果我在mysite.com/es/instructions它將「seleccionar」附加到無序列表的頂部,並且如果我在mysite.com/instructions它將「選擇」附加到無序列表的頂部。追加到基於URL的無序列表中
這裏是我開始和正常工作:
$("ul.gallery").prepend('<li class="gallery-item"><a href="#">Select</a></li>');
這裏是我嘗試添加根據網址:
$(function(){
var url = location.pathname;
if(url.indexOf('es') > -1){
$("ul.gallery").prepend('<li class="gallery-item"><a href="#">Selecctioner</a></li>');
}
else if (url.indexOf('') > -1) {
$("ul.gallery").prepend('<li class="gallery-item"><a href="#">Select</a></li>');
}
});
爲什麼這是行不通的任何想法。我甚至放了一個警報,而不是前置語句,看看它是否有效,它是否有效。
下面是HTML:
<ul id="gallery-1" class="gallery list columns_3 ">
<li class="gallery-item"> <a href="/instructions_en.pdf" title="English" class="gallery-icon"> English </a></li>
<li class="gallery-item"> <a href="/instructions_en.pdf" title="French" class="gallery-icon"> Spanish </a></li>
<li class="gallery-item gallery-endcol"> <a href="/instructions_en.pdf" title="German" class="gallery-icon"> German </a></li>
</ul>
什麼是應該發生的是,jQuery腳本添加「選擇」或「Selecctioner」,這取決於他們是什麼URL列表的頂部。
ur html代碼plz ?? – geekman
你確定location.pathname不包含'es'多個位置。 – Anoop
我不明白爲什麼這不應該工作。你是否在頁面加載後動態創建'ul'?這是一個帶有你的工作代碼的JSFiddle。 http://jsfiddle.net/46saG/ –