我正在尝试使用changePost函数更改x的值。JavaScript使用onclick()函数更改值
当changePost函数被触发时,它应该将x的值更改为 对应的posts [i]。
var x = 0; var title = posts[x].title, date = posts[x].date, content = posts[x].content, author = posts[x].author;
<article>
<h2>$title</h2>
<small>$date.format()</small>
<p>$content</p>
<cite>$author</cite>
</article>
for(var i in posts) { var title = posts[i].title, date = posts[i].date, content = posts[i].content, author = posts[i].author;
<article>
<h3 onclick="changePost()">$title</h3>
<small>$date.format()</small>
</article>
}
</aside>
function changePost(){ var x = i; };
谢谢。
嗨,你有没有这方面的一个JS小提琴? – 2014-11-23 10:26:38
另外,请将JS和标记分开。 – Terry 2014-11-23 10:31:40
Js小提琴:http://jsfiddle.net/bcq7580s/ – user3865871 2014-11-23 10:33:33