質問

In my database I have a row with article name and article link. I'm retrieving the information like this:

<div id="daily"></div>

$.getJSON(serviceURL + 'getmainnews.php', function(data){
  daily = data.items;
  $.each(daily, function(index, news){
    $('#daily').append('<a href="#" onclick="displaylink('+ news.link +');">' + news.article + '</a>');
  });
});
function displaylink(link){
  alert(link);
}

So all I want to do is get the link and pass it with onclick. I'm using Zepto.js. The news.article is displaying correctly but when I click on the link, I get this error:

"Uncaught SyntaxError: Unexpected token : main.html:1"

If my onclick parameters are empty, the onclick works. Is it a problem with the link? Does it not like the colon in the link? How can I make this work?

役に立ちましたか?

解決

try to change

onclick="displaylink('+ news.link +');" to onclick="displaylink(\''+ news.link +'\');"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top