2017-04-27 15 views
0

我有一個下拉菜單,只有當用戶從特定國家訪問網站時纔會顯示。在這個下拉列表中有兩個列表元素,每個列表元素都有錨標籤。我需要使用我的變量列表中的一些數據更改錨標記文本。需要更改<a>的文本來自變量列表中的文本

這裏是我的清單:

<div class="geo-location-button" style="text-decoration: none"><p>Take me to Global</p> 
    <ul class="geo-list"> 
     <li class="one"><a id="first" href="#\"> E-Commerce Portal </a></li> 
     <li class="two"><a id="second" href="#\"> Website </a></li> 
     </ul> 
    </div> 

這是我曾嘗試:

if (list[jo].url === undefined) { 
    $('.geo-list .one a').text(list[jo].text2); 
} 

這裏是叫 'JO'

'United States': { 
     text: "Take me to United States", 
     text2: "E-Commerce Portal", 
     text3: "Website", 
     url: '', 
     url2: 'https://www.bestbuy.com', 
     url3: 'http://www.google.com' 
     }, 


'United Kingdom': { 
     text: "Take me to UK", 
     url: 'http://www.google.co.uk' 
     }, 

由於我的變量列表的例子您可以從我的腳本中看到,只有當我的變量列表中的國家/地區沒有'url'的值,因此在兩個文本值(text2,text3)中都有多個選項。

回答

2

只是有一個錯字,你有one而不是.one

$('.geo-list .one a').text(list[jo].text2); 

另一件事,如果你的url''然後url === undefined將返回false。你想要的是url === undefined || url == ''

+0

我確實覺得自己像一個白癡不首先檢查,但它似乎仍然無法正常工作。我的代碼是這樣的:if(list [jo] .url === undefined){('。geo-list .one a')。text(list [jo] .text2); $('。geo-list .two a')。text(list [jo] .text3); } –

+1

好吧,很簡單,如果你的網址是'''',那麼'url === undefined'會返回'false'。你想要的是'url === undefined || url =='''。 –

+0

非常感謝您的支持。它非常完美!你可以添加到你的答案,所以我可以標記它正確 –

1

你錯過了.one類選擇器前的小圓點。

if (list[jo].url === undefined) { 
    $('.geo-list .one a').text(list[jo].text2); 
    $('.geo-list .two a').text(list[jo].text3); 
    ... etc 
} 
+0

是的,我已修復它,我的代碼看起來完全像你的。不知何故,它仍然沒有改變文字。 –

1

請使用。之前的一類同時在jquery中引用

if (list[jo].url === undefined) { 
    $('.geo-list .one a').text(list[jo].text2); 
}