+
95
-

回答

<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum=1.0,minimum=1.0,user-scalable=0" />

</head>

<body>
<div id="pardom">
<span id="firsubdom">子节点一</span>
<span id="secsubdom">子节点二</span>
</div>

<script type="text/javascript">
var getdomindex = function(el) {
if (!el) {
return -1;

}
var i = 0;
while ((el = el.previousElementSibling)) {
i++;
}
return i;
};

var emptysubdom = function(el) {
while (el.firstChild) {
el.removeChild(el.firstChild);
}
};

console.log(getdomindex(document.getElementById("secsubdom")));

emptysubdom(document.getElementById("pardom"));
</script>
</body>

</html>

网友回复

我知道答案,我要回答