当我输入以下代码时:
$('.whatever').html("LOL");
它有效。但在我正在构建的网站中,我需要添加的不仅仅是一个字符串。
这是我想添加的内容:
<div class="pic"><img class="left" src="img/happy-man.jpg"/ ></div>
<div class="pic"><img class="happy" src="img/happy-woman.jpg"/ ></div>
<div class="pic"><img class="right" src="img/happy-celeb.jpg"/ ></div>
但是当我将上面的 HTML 代码放入 html() 函数中时,什么也没有发生。我想知道我在语法方面犯了什么明显的错误。欣赏它!
最佳答案
$('.whatever').html('<div class="pic"><img class="left" src="img/happy-man.jpg"/ ></div><div class="pic"><img class="happy" src="img/happy-woman.jpg"/ ></div><div class="pic"><img class="right" src="img/happy-celeb.jpg"/ ></div>')
下面的代码将允许您替换div的html内容
或者你只是创建一个字符串
text= '<div class="pic"><img class="left" src="img/happy-man.jpg"/ ></div>'+'<div class="pic"><img class="happy" src="img/happy-woman.jpg"/ ></div>'
$('.whatever').html(text)
关于javascript - 我的 .html() 语法有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13821812/