选中的这段文字有可能是纯文字,也有可能是HTML文本。怎么样才能在它后面插入一些代码?
解决方案 »
- 看不懂这段代码,谁能给解释下
- 一个关于select标签的index默认选择问题
- 【分享】JavaScript播放MP3歌词同步显示
- 好站推荐:收集前端开发最有用资源,帮助你提升前端技术水平!
- 【求思路改进】JavaScript写的扫雷程序初稿,达不到预期效果
- 哪位知道js如何才能只启动默认浏览器,而不开启任何页面?
- 求!我要在<script language="JavaScript">写ASP代码
- 如何用js读取jsp网页中的列表字段值?
- 如何使reset对javascript处理过的值不清空
- 45分,我的全部分:css选择器,如何得到文本内容
- 一棵自己种的树(用oracle的图形化查询还挺管用的,种一棵树就一条sql搞定)
- 问一个关于分帧叶面赋值问题
<body>
<a href="#" onclick="javascript:abc.innerHTML='这里是插入的内容'">插入</a>
<div id=abc></div>
</body>
</html>
function a()
{
var rng=document.selection.createRange();
rng.text+="test";
}
</script>
<a href="#" onclick="a()">asdasd</a>加些文字。
可以参看这样的例子,不过上面的可以在你的选择之后加上HTML代码了。<HTML>
<BODY>
<H1 unselectable="on">Creating a Link and Retrieving the URL</H1>
<script>
function AddLink()
{//Identify selected text
var sText = document.selection.createRange();
if (!sText==""){
//Create link
document.execCommand("CreateLink");
//Replace text with URL
if (sText.parentElement().tagName == "A"){
sText.parentElement().innerText=sText.parentElement().href;
document.execCommand("ForeColor","false","#FF0033");
}
}
else{
alert("Please select some blue text!");
}
}
</script>
<P unselectable="on">Select any portion of the following blue text, such as "My favorite Web site". Click the button to turn the selected text into a link. The text will be changed to the URL that you specify.</P>
<P style="color=#3366CC">My favorite Web site is worth clicking on. Don't forget to check out my favorite music group!</P><BUTTON onclick="AddLink()" unselectable="on">Click to add link</BUTTON>
</BODY>
</HTML>
感谢你提供的这段程序,已经取得很大进展,但好象只能加入文本,不能插入HTML!如果要插入HTML该如何?
<script>
function add()
{
var a = document.createTextNode("text");
aa.appendChild(a);
}
</script>
</head><body><p id="aa" onmouseup="add()">111111111111111111132</p></body></html>
<BODY>
洒的非固定撒幅度撒发斯蒂芬洒的幅度所附算法规范化非好汉定界符国货<input onclick=alert(document.body.innerHTML)>
</BODY>
</HTML>
<script>
var htm="<input>"
document.onclick=function(){document.body.innerHTML+='<DIV style="position:absolute;left:'+event.clientX+';top:'+event.clientY+'">'+htm+'</div>'}
</script>
插入的时候位置是不能确定的啊。TO:sw47(我爱编程,脑筋好好,嗷嗷嗷嗷嗷...) 插入到BODY后面,程序如何判断用户选择的是哪个字?文字可能有很多个相同的字或者相同的话。