私はcontenteditableを使用する非常に単純なWYSIWYGエディタを持っています。それは正常に動作しますが、選択したテキストがリンクとして使用されているかどうかをテストします。 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>