我有一个var,里面有一些数据属性。
var test = {
"game": [
{
"Title": "blah",
"Type": "Sports"
}
]
}
我正在尝试在DOM上的h3中填充Title。这就是我目前正在尝试使用的内容:
$(function(){
$('#currentGame').val(test.game[0].Title);
});
当我这样做时,我没有收到错误但页面上没有任何内容。 #currentGame是h3的id。
答案 0 :(得分:4)
答案 1 :(得分:1)
假设您的网页包含H3#currentGame,并且您想设置其'title'属性,则可以
$(function(){
$('#currentGame').attr('title',test.game[0].Title);
});
另一方面,如果您只想设置H3的HTML内容,则< H3>之间的文本。和< / H3&gt ;,您使用:$('#currentGame').html(test.game[0].Title);
或$('#currentGame').text(test.game[0].Title);
如果您的标题的价值永远不会包含任何HTML。