var htmlStr = "<div class='"+name+"' id='"+name+i+"'><p></p></div>"; $("body").append(htmlStr); $("#"+name+i+" >p").text($(this).attr('title'));
上述为项目中实现此功能的部分代码.
.text(textString)方法和.html(htmlString)方法都是一样用来替换元素的内容,他们不同之处是:.html(htmlString) 方法会把HTML标签当作新的HTML标签来替换原来的内容,而.text(textString)则把HTML标签会转换成纯文本内容来代替元素的旧内 容。换句话说,.text(textString)方法中如果包含了HTML的标签,此方法会将< 替换为 <,>替换为>.因此可以把html的显示为纯文本的.
参考:http://www.w3cplus.com/jquery/html-text-value
http://api.jquery.com/text/