Jquery textarea在文本中添加链接
jquery textarea add link in text with costum popup
我有一个文本区,用户可以在那里输入一些文本,有时他们在那里使用一些html来使它看起来更好。有时这会出错,所以我试图通过添加一些带有函数的图像来简化它们。比如在<b> <i> <u> <del>
标签中包装文本。这很有效。现在我想让他们添加url,这就是我卡住的地方。
我想要的:
OK
后,我希望文本出现在用户离开光标的文本区域。文本区域中的文本需要看起来像<a href="' + url + '" rel="external">' + title + '</a>
。如果用户选择了一些文本,我希望它出现在弹出标题字段- 如果按下
add link
按钮,脚本需要查看文本区域中是否有选择 - 如果有选择,脚本会记住
- 弹出窗口打开
- 在弹出框的标题输入中显示选择。
- 用户填写url
- 用户按OK键
- 弹出窗口消失,所选文本被链接 所取代
- 链接看起来像
<a href="' + url + '" rel="external">' + title + '</a>
这是我的一些代码:
function wrapAsLink(url) {
var textArea = $('.area'),
len = textarea.value.length,
start = textarea.selectionStart,
end = textarea.selectionEnd,
sel = textarea.value.substring(start, end),
replace = '<a href="'+url+'" rel="external">' + sel + '</a>';
textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
$('.area').keyup();
}
和小提琴
你可以这样做:
小提琴。
把你的html改成:
<div class="editor">
<div class="toolbar">
<span id="btnedit-bold" title="Vergedrukte text"><img src="images/bold.png" /></span>
<span id="btnedit-italic" title="Italic text"><img src="images/italic.png" /></span>
<span id="btnedit-underline" title="Onderstreep text"><img src="images/underline.png" /></span>
<span id="divider"> </span>
<span id="btnedit-delete" title="verwijder (doorstreep) text"><img src="images/delete.png" /></span>
<span id="divider"> </span>
<span id="btnedit-link" title="Insert link"><img src="images/link.png" /></span>
</div>
<textarea name="editor-preview" class="area" placeholder="Uw bericht"></textarea>
</div>
<p> </p>
<div class="editor-preview"></div>
<div id="prompt">
<div class="prompt-background"></div>
<div class="prompt-dialog">
<div class="prompt-message">
<p><b>Insert Hyperlink</b></p>
</div>
<form class="prompt-form">
<p>titel</p>
<input id="btnedit-title" type="text" style="display: block; width: 80%; margin-right: auto; margin-left: auto;">
<p>http://example.com/</p>
<input id="btnedit-url" type="text" style="display: block; width: 80%; margin-right: auto; margin-left: auto;">
<button id="btnedit-ok" class="btn-orange" onClick="$('#prompt').show();">OK</button>
<button id="btnedit-cancel" class="btn-orange" onClick="$('#prompt').hide();">cancel</button>
</form>
</div>
</div>
并将它们添加到你的javascript中:
$('#btnedit-bold').on("click",function(e) {
wrapText('b');
});
$('#btnedit-italic').on("click",function(e) {
wrapText('i');
});
$('#btnedit-underline').on("click",function(e) {
wrapText('u');
});
$('#btnedit-delete').on("click",function(e) {
wrapText('del');
});
$('#btnedit-link').on("click",function(e) {
var textArea = $('.area'),
len = textArea.val().length,
start = textArea[0].selectionStart,
end = textArea[0].selectionEnd,
selectedText = textArea.val().substring(start, end);
$('#btnedit-title').val(selectedText);
$('#btnedit-url').val('http://');
$('#prompt').show();
});
$('#btnedit-ok').on("click",function(e) {
e.preventDefault();
$('#prompt').hide();
replacement = '<a title="'+$('#btnedit-title').val()+'" href="'+$('#btnedit-url').val()+'" rel="external">' + $('#btnedit-title').val() + '</a>';
wrapLink(replacement);
});
$('#btnedit-cancel').on("click",function(e) {
e.preventDefault();
$('#prompt').hide();
});
function wrapLink(link) {
var textArea = $('.area'),
len = textArea.val().length,
start = textArea[0].selectionStart,
end = textArea[0].selectionEnd,
selectedText = textArea.val().substring(start, end);
textArea.val(textArea.val().substring(0, start) + link + textArea.val().substring(end, len));
$('.area').keyup();
}
相关文章:
- 如何在Angular中的表的所有单元格中添加链接
- 如何使用 HTML/JavaScript 为对象添加链接
- 如何在滑块图像上添加链接
- HTML::向输入标记添加链接
- 正在向库图像添加链接
- 在旋转javascript横幅中添加链接
- 向每个堆叠条形图项目添加链接
- 根据课程自定义字段在课程页面上添加链接
- 如何使用java脚本添加链接
- 如何在D3元素中添加链接
- 如果满足条件,则使用JQuery向元素添加链接
- 当屏幕尺寸较小时,可使用Javascript向移动菜单添加链接.不再有效
- javascript下拉列表-无法添加链接
- Ext JS Gannt 在任务之间添加链接
- 添加链接时 JQuery 停止工作
- Javascript 在添加表行时添加链接文本
- 如何在 d3.js 行添加链接名称
- Umbraco 将从第 @nodename 页添加链接作为表单上的文本输入值
- 在jQuery插件中添加链接的方法
- 在可折叠内容标头(拆分链接)中添加链接 - 缺少 css