html是操作元素innerHTML属性, 直接操作这个属性会导致元素内部所有元素的事件绑定被清除
比如
$('div').append($('<span>').html('test').click(function(){alert('Click')}))
$('div').html($('div').html())
然後span上绑定的click事件就没了
after是在元素的同级到後面添加新元素
<div id="outer">
<div id="inner"></div>
</div>
$('#inner').after('<span>')
能把一个标签替换成另一个标签的是
replaceAll和replaceWith
html并不会把<div>变成<h2>
Html中特殊字符不被转义,可以使用预格式化标签。pre 是 Preformatted text(预格式化文本) 的缩写。使用此标签可以把代码中的空格和换行直接显示到页面上。例如HTML代码:1
2
3
4
5
<pre>
if (xx >5) {
print "比5大!\n"
}
</pre>
浏览器显示效果:if (xx >5) {print "比5大!\n"}<textarea></textarea>之间包含有类似的这种转义字符的时候总会被解析,倒是可以把所有的"&"通过程序替换成"&",但是有些本来就是"&"的也会被转换,这就错了。如何让<textarea></textarea>之间包含的文本原封不动的显示出来呢?总结如下:解决方法有两种:第1种:
1
2
3
4
5
6
<body>
<textarea id='t' rows=20 cols=20></textarea>
<script>
document.getElementById('t').innerText='a<&>'
</script>
</body>
第2种:/*将字串转为html格式*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public String strToHtml(String s)
{
if (s==null||s.equals("")) return ""
s = s.replaceAll("&", "&")
s = s.replaceAll("<", "<")
s = s.replaceAll(">", ">")
s = s.replaceAll(" ", " ")
// s = s.replaceAll("/n", "")
// s = s.replaceAll("'", "'")
return s
}
/*将html格式转为字串*/
public String strToHtml(String s)
{
if (s==null||s.equals("")) return ""
s = s.replaceAll("&","&")
s = s.replaceAll("<","<")
s = s.replaceAll(">",">")
s = s.replaceAll(" "," ")
//s = s.replaceAll("","/n")
//s = s.replaceAll("'","'")
return s
}
最后一点:jQuery的.html()方法默认会转义的,这种情况使用.text()就不会转义了。
载入远程 html 文件代码并插入至 dom 中。默认使用 get 方式 - 传递附加参数时自动转换为 post 方式。jq 1.2 中,可以指定选择符,来筛选载入的 html 文档,dom 中将仅插入筛选出的 html 代码。语法形如 url #some selector。请查看示例。----------------------------------------load html from a remote file and inject it into the dom.a get request will be performed by default - but if you pass in any extra parameters then a post will occur. in jq 1.2 you can now specify a jq selector in the url. doing so will filter the incoming html document, only injecting the elements that match the selector. the syntax looks something like url #some selector. see the examples for more information.返回值jq参数url (string) : 待装入 html 网页网址。data (map) : (可选) 发送至服务器的 key/value 数据。callback (callback) : (可选) 载入成功时回调函数。示例加载文章侧边栏导航部分至一个无序列表。html 代码:<bjq links:</b<ul id=links</uljq 代码:$(#links).load(/main_page #p-getting-started li)----------------------------------------加载 feeds.html 文件内容。jq 代码:$(#feeds).load(feeds.html)----------------------------------------同上,但是以 post 形式发送附加参数并在成功时显示信息。