I'm making a app that displays random countries and their national dishes.
the dish is wrapped in a <a>
tag and I want the user to be able to click on the dish name and then automatically search for it on google.
I'm having troubles with the url in the <a>
tag.
the problem is in this line document.getElementById('countries').innerHTML = 'Country: ' + strRandCountry + '<br> ' + 'Dish:' + '<a href="https://www.google.com/search?q=" + strRandDish,"_blank">' + strRandDish;
can someone please help me
function randomCountries(){
var random = random_countries(countries_data);
var strRandCountry = JSON.stringify(random.country).replace(/"/g,'');
var strRandDish = JSON.stringify(random.dish).replace(/"/g,'');
var search = ''
var li = '<li>';
sel_count_arr.push( '<li>' + 'Country: ' + strRandCountry + '<br>' + 'Dish: ' + '<a href="">' + strRandDish + '</a>');
document.getElementById('countries').innerHTML = 'Country: ' + strRandCountry + '<br> ' + 'Dish:' + '<a href="https://www.google.com/search?q=" + strRandDish,"_blank">' + strRandDish;
document.getElementById('selected_countries').innerHTML = sel_count_arr.join(', ');
}
function random_countries(countries_data){
return countries_data[Math.floor(Math.random() * data.length)];
}
var countries_data = data, sel_count_arr =[];
答案 0 :(得分:1)
连接时使用的引号类型错误。您还忘记了关闭a
标签。
这应该对您有用:
document.getElementById('countries').innerHTML =
'Country: ' + strRandCountry + '<br> Dish: <a href="https://www.google.com/search?q=' + strRandDish + '" target="_blank"> ' + strRandDish + '</a>';
您还可以执行template literal,我认为它更易于阅读
document.getElementById('countries').innerHTML =
`Country: ${strRandCountry}<br> Dish: <a href="https://www.google.com/search?q=${strRandDish}" target="_blank">${strRandDish}</a>`;