Javascript 单击div类时将文本插入文本字段
我现在正在创建这个提交系统。我想创建它,以便在单击div时插入一些文本(如Javascript 单击div类时将文本插入文本字段,javascript,php,html,textfield,Javascript,Php,Html,Textfield,我现在正在创建这个提交系统。我想创建它,以便在单击div时插入一些文本(如[b][/b])。这是我们正在使用的示例: <div class="insertBBC" title="Bold Text" onClick="moveNumbers([b][/b])" id="bold"> <p>B</p> </div>
[b]
[/b]
)。这是我们正在使用的示例:
<div class="insertBBC" title="Bold Text" onClick="moveNumbers([b][/b])" id="bold">
<p>B</p>
</div>
<div class="insertBBC" title="Italic Text">
<p>I</p>
</div>
<div class="insertBBC" title="Bullet Points">
<p>BP</p>
有线索吗?谢谢:)不幸的是,您唯一的选择是使用jQuery或Javascript(或其他浏览器端库)。PHP只处理服务器端代码,可以输出您想要使用的html字段,但在页面加载到浏览器后,它无法与元素交互 我能 我没有向您解释如何使用jQuery,只是说现在开始学习它是非常值得的。用它做简单的事情很容易,所以不要害怕 以下是我的建议: 使用此标记加载最新的jQuery库:
<script src="http://code.jquery.com/jquery-latest.js"></script>
您还可以使用此函数获取所选文本,我在这里得到:
然后可以使用函数.before().html(“[b]”)和.after().html(“[/b]”)将标记放置在需要的位置
我希望这能帮助你了解你需要做什么
干杯 正如Jason Fingar所提到的,TinyMCE()是一个很好的解决方案,但可能需要一些javascript才能使其动态工作,就像我认为您的目标一样。CKEditor()也是一个不错的选择。
<script src="http://code.jquery.com/jquery-latest.js"></script>
$("#boldButtonID").click(function(){
//get the index of the cursor
//select the word that the cursor is on by stopping at the space (" ") character in either direction, then insert [b] and [/b] in the appropriate locations.
}
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}