我有一个简单的标题,如果它与 ID 匹配,我想用城市名称填充它。我目前正在使用replaceWith
,它似乎只在第一次工作。不知道为什么?
这是功能:
function toggleDetails() {
$(".pin").click(function() {
var cityName = $(this).attr('id');
$(".city-details").toggle();
if (cityName === 'Miami') {
$('h3#cityName').replaceWith("Miami");
}
else if(cityName === 'Philadelphia') {
$('h3#cityName').replaceWith("Philadelphia");
}
else if (cityName === 'Chicago') {
$('h3#cityName').replaceWith("Chicago");
}
else if (cityName === 'Los-Angeles') {
$('h3#cityName').replaceWith("Los Angeles");
}
else if (cityName === 'Houston') {
$('h3#cityName').replaceWith("Houston");
}
else if (cityName === 'San-Diego'){
$('h3#cityName').replaceWith("San Diego");
}
else if (cityName === 'Atlanta') {
$('h3#cityName').replaceWith("Atlanta");
}
console.log(cityName);
});
}
HTML:
<p class="pin totally newyork" id="NewYork">
New York
</p>
<p class="pin barely boston" id="Boston">
Boston
</p>
<p class="pin fairly philadelphia" id="Philadelphia">
Philadelphia
</p>
<p class="pin barely chicago" id="Chicago">
Chicago
</p>
<p class="pin barely la" id="Los-Angeles">
Los Angeles
</p>
<p class="pin fairly houston" id="Houston">
Houston
</p>