我想用jQuery来调整复制机制。我注意到当选择的锚点和焦点节点相同时,相应的jQuery对象(使用.is
方法进行比较)不是。
在Chrome和Firefox(Windows)中,此示例中的比较失败:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$(function () {
$("#testdiv").on("copy", function(event) {
var sel = window.getSelection();
var $anchorNode = $(sel.anchorNode);
var $focusNode = $(sel.focusNode);
$("#logdiv").append("<p>The DOM nodes are" + (sel.anchorNode == sel.focusNode ? "" : "<b> not</b>") + " the same.</p>");
$("#logdiv").append("<p>The jQuery objects are" + ($anchorNode.is($focusNode) ? "" : "<b> not</b>") + " the same.</p>");
$("#logdiv").append("<p>Parent: the DOM nodes are" + (sel.anchorNode.parent == sel.focusNode.parent ? "" : "<b> not</b>") + " the same.</p>");
$("#logdiv").append("<p>Parent: the jQuery objects are" + ($anchorNode.parent().is($focusNode.parent() ) ? "" : "<b> not</b>") + " the same.</p>");
});
});
</script>
</head>
<body>
<div id="testdiv">
Select some text inside this div and press Ctrl+C.
</div>
<div id="logdiv" style="border: 1pt solid black;"><p><i>Log</i></p></div>
</body>
</html>
我准备了jsFiddle来说明这一点,但无法在那里重现。
所以我尝试使用SO的代码段小部件。它又有效了:
$(function () {
$("#testdiv").on("copy", function(event) {
var sel = window.getSelection();
var $anchorNode = $(sel.anchorNode);
var $focusNode = $(sel.focusNode);
$("#logdiv").append("<p>The DOM nodes are" + (sel.anchorNode == sel.focusNode ? "" : "<b> not</b>") + " the same.</p>");
$("#logdiv").append("<p>The jQuery objects are" + ($anchorNode.is($focusNode) ? "" : "<b> not</b>") + " the same.</p>");
$("#logdiv").append("<p>Parent: the DOM nodes are" + (sel.anchorNode.parent == sel.focusNode.parent ? "" : "<b> not</b>") + " the same.</p>");
$("#logdiv").append("<p>Parent: the jQuery objects are" + ($anchorNode.parent().is($focusNode.parent() ) ? "" : "<b> not</b>") + " the same.</p>");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="testdiv">
Select some text inside this div and press Ctrl+C.
</div>
<div id="logdiv" style="border: 1pt solid black;"><p><i>Log</i></p></div>
有关正在发生的事情的任何想法?