-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInnerTextExample05.htm
executable file
·47 lines (41 loc) · 1.42 KB
/
InnerTextExample05.htm
1
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
<!DOCTYPE html>
<html>
<head>
<title>InnerText Example 5</title>
</head>
<body>
<div id="content">
<p>This is a <strong>paragraph</strong> with a list following it.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
<input type="button" value="Replace InnerText Simple" onclick="replaceTextSimple()">
<input type="button" value="Replace InnerText Complex" onclick="replaceTextComplex()">
<script type="text/javascript">
function getInnerText(element){
return (typeof element.textContent == "string") ?
element.textContent : element.innerText;
}
function setInnerText(element, text){
if (typeof element.textContent == "string"){
element.textContent = text;
} else {
element.innerText = text;
}
}
function replaceTextSimple(){
var div = document.getElementById("content");
setInnerText(div, "Hello world!");
alert(getInnerText(div));
}
function replaceTextComplex(){
var div = document.getElementById("content");
setInnerText(div, "Hello & welcome, <b>\"reader\"!</b>");
alert(getInnerText(div));
}
</script>
</body>
</html>