需要更改<a> with text from a variable list

时间:2017-04-27 06:59:02

标签: javascript jquery

I have a drop down that only shows up when an user access the website from a specific country. In this drop down there are two list elements with anchor tags in each. I need to change the anchor tags text with some data from my variable list.

Here is my list:

<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>

Here is what I have tried:

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

Here is an example of my variable list called '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'
      }, 

As you can see from my script, I want to change the text of the anchor within the list only when a country from my variable list does not have a value for 'url', therfore having more than one options in both text values (text2,text3).

3 个答案:

答案 0 :(得分:2)

只是一个错字,你有one而不是.one

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

如果您的url'',则url === undefined将返回false,这是另一回事。你想要的是url === undefined || url == ''

答案 1 :(得分:1)

您错过了.one班级选择器前面的点。

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

答案 2 :(得分:1)

请使用。在jquery中引用类之前

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