我有一个非常简单的所见即所得编辑器,使用contenteditable实现。它工作正常,但我想测试所选文本是否被用作链接。当我使用document.queryCommandState('CreateLink')时,即使文本位于锚点内,它也总是返回false。如下面的示例。
我做错了什么,还是有其他方法可以测试文本是否当前用作链接?
<script>
function testLink () {
// check if this is a link
var state = document.queryCommandState('CreateLink');
alert(state);
// create the link
document.execCommand ('CreateLink', false, 'http://www.example.com');
}
</script>
<div contenteditable="true">Here is some sample text to test with.</div>
<br /><br />
<button onclick="testLink();">Test the state of the create link command</button>