我有一个下拉菜单,仅当用户从特定国家/地区访问该网站时才会显示。在此下拉列表中,有两个列表元素,每个元素中都带有 anchor 标记。我需要使用变量列表中的一些数据更改 anchor 标记文本。
这是我的 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>
这是我尝试过的:
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”值时,我才想更改列表中 anchor 的文本,因此两个文本中都有多个选项值(文本2,文本3)。
最佳答案
只是一个拼写错误,您有 one
而不是 .one
。
$('.geo-list .one a').text(list[jo].text2);
另一件事是,如果您的 url
是 ''
,那么 url === undefined
将返回 false
。你想要的是 url === undefined || url == ''
关于javascript - 需要使用变量列表中的文本更改 <a> 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43650711/