将函数应用于所选文本 [英] apply function to selected text

查看:56
本文介绍了将函数应用于所选文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://webwrinkle.com/

我在上面列出的网址上有一个基本表格。我希望用户可以选择将javascript或jquery函数应用于表单中的选定文本。是一个php文件,因此信息将进入数据库,然后在页面中以html格式输出。

I have a basic form up on the above listed web address. I would want the option for the user to be able to apply a javascript or jquery function to selected text in the form. Is a php file, so the info would go into a database, and then output as html in a page.

我以为用户可以选择输入到表单中的文本,通过按钮应用功能,并且与按钮绑定的功能会在两端输入链接标签所选文本的选择,以使所选文本成为已发布的php页面上的可点击网络链接。 (TinyMCE中的相同功能)如果需要更多信息,或者我没有完全解释,请告诉我。如果能够朝着正确的方向开始,我应该能够弄清楚如何构建需要应用的PHP函数...

I was thinking that the user could select text entered into the form, apply the function via a button, and the function tied to the button would input link tags on both ends of the selected text to make the selected text a clickable weblink on a posted php page. (the same kind of functionality in TinyMCE) Let me know if more info is needed, or I didn't explain it fully. I should be able to figure out how to build the PHP functions that need applied if I can get started in the right direction...

感谢您的帮助!

推荐答案

首先,在您准备提交备忘录之前,您不必向服务器发送任何内容 - 您可以使用javascript完成所有编辑。

Well first of all, you shouldn't have to send anything to the server until you're ready to submit the "memo" -- you can do all of the editing you described entirely with javascript.

首先,您需要能够检索当前选定的文本。您可以使用以下内容:

First of all, you need to be able to retrieve the currently selected text. You can use something like this:

var memoBox = document.getElementById("memo"); //get the memo input element

var startIndex = memoBox.selectionStart, endIndex = memoBox.selectionEnd; //store the start and end of the user's selection
var selectedText = memoBox.value.substring(startIndex,endIndex); //retrieve and store the selected text

现在,假设您要添加指向所选文本的链接文本。您可以编写如下函数:

Now, let's say you want to add a link to the selected text. You could write a function like this:

function addLinkToSelected() {
    var memoBox = document.getElementById("memo");
    var startIndex = memoBox.selectionStart, endIndex = memoBox.selectionEnd;
    var selectedText = memoBox.value.substring(startIndex,endIndex);

    var linkURL = prompt("Link target: "); //ask the user for a link target
    var linkTag = '<a href="'+linkURL+'">'; //construct a link tag

    memoBox.value = memoBox.value.substring(0,startIndex) + linkTag 
                   + selectedText + "</a>" + memoBox.value.substring(endIndex+1);
}

现在,只需添加一个按钮并在单击时调用该函数: / p>

Now, just add a button and call the function when it is clicked:

<input type="button" value="add link" onclick="addLinkToSelected()" />

我不确定这是否是你想要的,所以随意添加更多详情。我已经创建了一个 jsfiddle ,您可以查看一个有效的示例。

I'm not exactly sure if this is what you're looking for, so feel free to add more detail. I've created a jsfiddle where you can check out a working example.

这篇关于将函数应用于所选文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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