我使用下面的代码在a范围内插入一个链接。
我有以下函数,该函数由一个按钮调用以保存选定的文本。然后显示linkBar div,其中包含一个用于链接插入的文本输入。
注意:当我专注于文本输入中的输入文本时,我失去了选择,所以我尝试使用toggleRange()而不是toggleSelection。
function linkIt(){
if (savedSel) {
rangy.removeMarkers(savedSel);
}
savedSel = rangy.saveSelection();
savedSelActiveElement = document.activeElement;
$("#linkBar").css({"display":"block", "top": "150px", "left": "500px"});}
在此之后,我有下面的代码,一旦在相同的div中单击一个按钮,就会执行该代码,这一点由前面的函数显示。
submitLink.ontouchstart = submitLink.onmousedown = function() {
var linkName = $("#linkText").val();
toggleLink(linkName);
$("#linkBar").css({"display":"none"});
if (savedSel) {
rangy.restoreSelection(savedSel, true);
savedSel = null;
gEBI("restoreButton").disabled = true;
window.setTimeout(function() {
if (savedSelActiveElement && typeof savedSelActiveElement.focus != "undefined") {
savedSelActiveElement.focus();
}
}, 1);
}
return false;
}此函数调用以下函数将linkApplier应用于选定的范围。但不起作用
function toggleLink(linkName) {
linkApplier = rangy.createCssClassApplier("link", {
elementTagName: "a",
elementProperties: {
href: linkName,
title: "Rangy home page",
target: "_self"
}
});
//linkApplier.toggleSelection();
linkApplier.toggleRange(savedSel);
}发布于 2013-02-11 01:13:58
在选择恢复之后,您需要调用toggleSelection()。我认为以下几点将解决这个问题(未经测试):
submitLink.ontouchstart = submitLink.onmousedown = function() {
var linkName = $("#linkText").val();
$("#linkBar").css({"display":"none"});
if (savedSel) {
rangy.restoreSelection(savedSel, true);
toggleLink(linkName);
savedSel = null;
gEBI("restoreButton").disabled = true;
window.setTimeout(function() {
if (savedSelActiveElement && typeof savedSelActiveElement.focus != "undefined") {
savedSelActiveElement.focus();
}
}, 1);
}
return false;
};https://stackoverflow.com/questions/14793858
复制相似问题