Javascript JQuery在文本末尾单击时删除完整链接
我正在使用jquery删除链接。我需要一些代码方面的帮助,因为当我单击文本末尾并单击按钮删除同一行中的完整链接时,我很难删除链接 当我尝试这个:Javascript JQuery在文本末尾单击时删除完整链接,javascript,jquery,html,Javascript,Jquery,Html,我正在使用jquery删除链接。我需要一些代码方面的帮助,因为当我单击文本末尾并单击按钮删除同一行中的完整链接时,我很难删除链接 当我尝试这个: else if (window.getSelection().getRangeAt(0)) { document.execCommand('unlink', false, selected_text); } 它不会删除链接,除非我必须突出显示文本并单击按钮删除链接 以下是完整的代码: $(document).on('click', '#too
else if (window.getSelection().getRangeAt(0))
{
document.execCommand('unlink', false, selected_text);
}
它不会删除链接,除非我必须突出显示文本并单击按钮删除链接
以下是完整的代码:
$(document).on('click', '#toolbar_unlink', function(e) {
e.preventDefault();
var selected_text = window.getSelection() ? "" + window.getSelection() : document.selection.createRange().text;
if (selected_text) {
document.execCommand('unlink', false, selected_text);
}
else if (window.getSelection().getRangeAt(0))
{
selected = window.getSelection().getRangeAt(0).endContainer.wholeText;
console.log(window.getSelection().getRangeAt(0));
document.execCommand('unlink', false, selected_text);
//clearSelection();
}
});
当我突出显示文本并单击按钮时,它将毫无问题地删除链接。因此,当我在文本末尾单击并单击按钮时,它不会删除完整链接
但是,当我点击文本末尾时,我将获得全文视频2
,没有链接
以下是html:
<div id="toolbar" class="toolbar_top" role="toolbar" style="height: 100px; user-select: none;">
<button id="toolbar_unlink" class="toolbar_button toolbar_button_unlink_icon" command="+unlink" data-toggle="tooltip" data-placement="bottom" title="Remove link (CTRL+SHIFT+F9)" style="user-select: none; position: relative; margin-left: 3px; margin-top: 13px; outline: none; z-index: 0;" button="" type="button"></button>
</div>
<div id="text_editor" class="editor_message" hidefocus="false" aria-label="Message Body" g_editable="true" role="textbox" aria-hidden="true" aria-multiline="true" contenteditable="true" tabindex="1" style="direction: ltr; height: 500px; width: 100%; padding-left: 25px; padding-top: 18px; font-size: 16px; border-bottom: 1px solid #d1d1d1; border-right: 1px solid #d1d1d1; border-left: 1px solid #d1d1d1; overflow-y: auto;" itacorner="6,7:1,1,0,0">
<div><a href="https://www.youtube.com/watch?v=dHyIq3boCu0" target="_blank" data-saferedirecturl="https://www.google.com/url?q=https://www.youtube.com/watch?v%3DdHyIq3boCu0&source=gmail&ust=1577810994184000&usg=AFQjCNFaXvqSKIPAwOUGOicDn-cMTWvW_w">Video Here 1!</a></div>
<div><a href="http://www.google.com/" target="_blank" data-saferedirecturl="https://www.google.com/url?q=http://www.google.com/&source=gmail&ust=1577810994184000&usg=AFQjCNEaLRLcUmjjRiAPYptws9xd7P5A5w">Video Here 2!</a></div><div></div>
</div>
当我试着这样做的时候:
$('#link').contents().unwrap()
它将删除所有的链接时,我只想删除一个链接,当我点击文本或在链接的结尾
你能给我举一个例子,当我点击文本末尾并点击按钮删除链接时,我如何删除链接
谢谢。还包括您尝试过的内容。@justDan我尝试过
document.execCommand('unlink',false,selected\u text)代码>到目前为止没有luck@justDan我还尝试了$('#link').contents().unwrap()代码>它将删除所有链接。当我点击链接所在的文本时,我只想删除一个链接。你能给我们发送完整的html(带)按钮吗?好的,当你点击按钮删除链接时,你会取消选择以前选择的任何文本,这就很好地解释了为什么它不起作用。我认为你有更多的用户体验问题,而不是编码问题。
document.execCommand('unlink', false, selected_text);