[轉] 關于 insertAdjacentHTML insertAdjacentText
1
添加html內容(insertAdjacentHTML和insertAdjacentText)
2
dhtml提供了兩個方法來進行添加,insertAdjacentHTML和insertAdjacentText
3
insertAdjacentHTML方法:在指定的地方插入html標簽語句。
4
原型:insertAdjacentHTML(swhere,stext)
5
參數:
6
swhere:指定插入html標簽語句的地方,有四種值可以用:
7
1.beforeBegin:插入到標簽開始前
8
2.afterBegin:插入到標簽開始標記后
9
3.beforeEnd:插入到標簽結束標記前
10
4.afterEnd:插入到標簽結束標記后
11
stext:要插入的內容
12
例:var sHTML="<input type=button onclick=" + "go2()" + " value='Click Me'><BR>"
13
var sScript='<SCRIPT DEFER>'
14
sScript = sScript + 'function go2(){ alert("Hello from inserted script.") }'
15
sScript = sScript + '</script' + '>';
16
ScriptDiv.insertAdjacentHTML("afterBegin",sHTML + sScript);
17
在html正文中加入一行:
18
<DIV ID="ScriptDiv"></Div>
19
最終變成:
20
<DIV ID="ScriptDiv">
21
<input type=button onclick=go2() value='Click Me'><BR>
22
<SCRIPT DEFER>
23
function go2(){alert("Hello from inserted sctipt.")}'
24
</script>
25
</DIV>
26
insertAdjacentText方法與insertAdjacentHTML方法類似,只不過只能插入純文本,參數相同
27
trackback:http://gmfzh.bokee.com/3680267.html
28
29
30
方法名稱:insertHtml(where,el,html)
31
32
參數介紹:
33
where:插入位置。包括beforeBegin,beforeEnd,afterBegin,afterEnd。
34
el:用于參照插入位置的html元素對象
35
html:要插入的html代碼
36
37
源碼如下:
38
<script type="text/javascript">
39
<!--
40
function insertHtml(where, el, html){
41
where = where.toLowerCase();
42
if(el.insertAdjacentHTML){
43
switch(where){
44
case "beforebegin":
45
el.insertAdjacentHTML('BeforeBegin', html);
46
return el.previousSibling;
47
case "afterbegin":
48
el.insertAdjacentHTML('AfterBegin', html);
49
return el.firstChild;
50
case "beforeend":
51
el.insertAdjacentHTML('BeforeEnd', html);
52
return el.lastChild;
53
case "afterend":
54
el.insertAdjacentHTML('AfterEnd', html);
55
return el.nextSibling;
56
}
57
throw 'Illegal insertion point -> "' + where + '"';
58
}
59
var range = el.ownerDocument.createRange();
60
var frag;
61
switch(where){
62
case "beforebegin":
63
range.setStartBefore(el);
64
frag = range.createContextualFragment(html);
65
el.parentNode.insertBefore(frag, el);
66
return el.previousSibling;
67
case "afterbegin":
68
if(el.firstChild){
69
range.setStartBefore(el.firstChild);
70
frag = range.createContextualFragment(html);
71
el.insertBefore(frag, el.firstChild);
72
return el.firstChild;
73
}else{
74
el.innerHTML = html;
75
return el.firstChild;
76
}
77
case "beforeend":
78
if(el.lastChild){
79
range.setStartAfter(el.lastChild);
80
frag = range.createContextualFragment(html);
81
el.appendChild(frag);
82
return el.lastChild;
83
}else{
84
el.innerHTML = html;
85
return el.lastChild;
86
}
87
case "afterend":
88
range.setStartAfter(el);
89
frag = range.createContextualFragment(html);
90
el.parentNode.insertBefore(frag, el.nextSibling);
91
return el.nextSibling;
92
}
93
throw 'Illegal insertion point -> "' + where + '"';
94
}
95
96
trackback: http://www.aygfsteel.com/Scott/archive/2007/08/30/141449.html
97

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

posted on 2009-07-09 10:37 星期五 閱讀(374) 評論(0) 編輯 收藏 所屬分類: web 開發