以标记包围选定的文本 [英] Surrounding selected text with tags

查看:118
本文介绍了以标记包围选定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<ul class="contact">
<li class="first">Carnegie Mellon University</li>
<li>5000 Forbes Avenue, Pittsburgh, PA 15213</li>
</ul>

=>

<ul class="contact">
<li class="first">Carnegie Mellon University</li>
<li>[address]5000 Forbes Avenue, Pittsburgh, PA 15213[/address]</li>
</ul>

想象我在浏览器中打开一个网页,找到一些信息在页面非常有用。所以我想选择有用的信息(例如,Carnegie Mellon大学的地址)的页面,然后点击一个将要实施的注入标记按钮。其结果是,该页面的源代码code将围绕与用户选择的文本对标签(例如,[地址] [/地址])注入。然后,我将节省注入源$ C ​​$ C作进一步处理。

Imagine I open a web page in a browser and find some info useful in the page. So I'd like to select the useful info (e.g., the address of Carnegie Mellon University) from the page and click a TO-BE-IMPLEMENTED "inject tag" button. As a result, the source code of the page will be injected with a pair of tags (e.g., [address][/address]) surrounding the user selected text. Then I will save the injected source code for further processing.

谁能帮助建议实施这样的功能的一种方式?我真的很新的JavaScript的东西,所以请你的建议有点详细。

Can anyone help suggest a way of implementing such a function? I'm really new to Javascript stuff so please be a little detailed with your suggestion.

推荐答案

在谷歌搜索,我发现一个例子,你可以将选定的文本:的http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-selected-text.html.

Search in google, I find an example where you can get the selected text: http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-selected-text.html.

有了这个,你只需要更改警报操作插入你想要的标签。要使用jQuery做到这一点,请使用以下code:

With this, you only need to change the alert action to insert the tags you want. To do this with jquery, use the following code:

$(selected).text("[tag]" + $(selected).text() + "[/tag]");

在这里选择的必须是重新presents您所选文本的变量。

where selected must be the variable that represents your selected text.

这篇关于以标记包围选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆