这段代码 在Internet Explorer中无效。有什么替代方案吗?
<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {
border: 1px solid black;
}
</style>
</head>
<body>
<div id="myDIV">
<p>I am a p element inside div, and I have a <span id="mySPAN">span</span> element inside of me.</p>
</div>
<p>I am a p element inside div, and I have a <span id="mySPAN">span</span> element inside</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var span = document.getElementById("mySPAN");
var text = span.childNodes[0];
var div = document.getElementById("myDIV").contains(text);
document.getElementById("demo").innerHTML = div;
}
</script>
</body>
</html>
function getRange(root): Range {
const sel = window.getSelection();
const range = sel.getRangeAt(0);
if (range && range.commonAncestorContainer !== root && root.contains(range.commonAncestorContainer)) {
return range.cloneRange();
}
return null;
}
我想检查 div 元素是否包含所选范围:
<span id="mySPAN">span</span>
,你应该知道 ID 不能重复。 - 31piy