在线文本编辑器中,如何实现的对选定的内容加HTML代码?

keephope 2007-09-28 10:42:11
我能找出被选中的内容

我想实现的是

选择一部分内容,然后点按钮(例如加粗),实现对那段文字的操作(结果是那段文字加粗了,但是其他文件依然保持原样)

text.htm
<body canHaveHTML="true">
</body>


edit.htm
<table width="500">
<tr>
<td>
<input type="button" value="加粗" onclick="jiacu()">
</td>
<td>
<input type="button" value="获得信息" onclick="alert(tempframe.document.getElementsByTagName('BODY')[0].innerText)">
</td>
<td>
<input type="button" value="获得选中的信息" onclick="alert(tempframe.document.getElementsByTagName('BODY')[0].document.selection.createRange().htmlText)">
</td>
</tr>
<tr>
<td colspan="3">
<iframe src="text.htm" id="tempframe" name="tempframe" width="800" scrolling="yes"></iframe>
<script language="javascript">
window.frames["tempframe"].document.designMode="On";
</script>
</td>
</tr>
</table>


<script>
function jiacu()
{
tempframe.document.getElementsByTagName('BODY')[0].innerHTML="<strong>"+tempframe.document.getElementsByTagName('BODY')[0].innerHTML+"</strong>"
}
</script>

我只能实现全部加粗,不知道局部加粗怎么弄
...全文
90 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧