【原生JS】评论编辑器 文本操作
效果图:

HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script src="js/script.js"></script>
</head>
<body>
<div class="body">
<div class="box">
<form method="" action="">
<div class="pl"><div>评论 <span>7</span></div></div>
<div class="wbk">
<div class="tx"></div>
<div class="srk">
<div class="srknr"><textarea class="area" id="txtarea">你的评论可以一针见血</textarea></div>
<div class="s-button">
<input type="button" class="jc btn" onclick="bold()" /> //使用ul li标签一样可行
<input type="button" class="qx btn" onclick="italic()"/>
<input type="button" class="syh btn" onclick="Quotes()"/>
<input type="button" class="scx btn" onclick="linetrough()"/>
<input type="button" class="xhx btn" onclick="underline()"/>
</div>
<div class="button"><input type="button" class="tj-button" name="" id="" value="提交评论" /></div>
</div>
</div>
<div class="add">
<div class="text"><input type="text" name="name" class="texts" id="name" value="昵称" onclick = "change('name')"/></div>
<div class="text"><input type="text" name="email" class="texts" id="email" value="邮箱" onclick = "change('email')"/></div>
<div class="text"><input type="text" name="site" class="texts" id="site" value="网址" onclick = "change('site')"/></div>
<div class="label"><div class="lab">昵称(必填)</div><div class="lab">邮箱(必填)</div><div class="lab">网址</div></div>
</div>
</form>
</div>
</div>
</body>
</html>
布局CSS:
*{padding:;margin:;}
.body{ border:1px solid #dddddd; width:1200px; height:1000px; margin:0 auto; box-shadow:0px 0px 50px gainsboro;}
.box{width:818px; height:408px; border:1px solid #dddddd; margin:100px auto; box-shadow:0px 0px 50px -2px gainsboro;}
.box .pl{width:776px; height:40px; border:1px solid #eaeaea; margin:25px 0 0 20px;}
.box .pl div{font:18px/40px "微软雅黑"; margin-left:10px;}
.box .pl span{color:red;}
.box .wbk{width:778px; height:116px; margin:15px 0 0 20px; position:relative;}
.box .wbk .tx{width:46px; height:114px; background: url(../img/header.gif) no-repeat;}
.box .wbk .srk{width:728px; height:112px; border:2px solid #ccd4d9; position:absolute; top:; right:;}
.box .wbk .srk .srknr{width:728px; height:76px; }
.box .wbk .srk .area{width:728px; height:76px; border:none; font:12px/24px "微软雅黑"; color:#c6b1a9; text-indent:6px;}
.box .wbk .srk .s-button{width:630px; height:35px; border-top:1px solid #f2f2f2; float:left; padding-top:8px; padding-left:10px;}
.box .wbk .srk .s-button .btn{margin-right:12px; width:12px; height:12px; border:none;}
.box .wbk .srk .s-button .wx{background:url(../img/wx.gif) no-repeat;} //使用图片整合技术会更好
.box .wbk .srk .s-button .jc{background:url(../img/bold.gif) no-repeat;}
.box .wbk .srk .s-button .qx{background:url(../img/italic.gif) no-repeat;}
.box .wbk .srk .s-button .syh{background:url(../img/syh.gif) no-repeat;}
.box .wbk .srk .s-button .scx{background:url(../img/font.gif) no-repeat;}
.box .wbk .srk .s-button .xhx{background:url(../img/u.gif) no-repeat;}
.box .wbk .srk .s-button .dm{background:url(../img/file.gif) no-repeat; width:12px; height:14px;}
.box .wbk .srk .s-button .tp{background:url(../img/p.gif) no-repeat; width:15px; height:14px;}
.box .wbk .srk .button{width:100px; height:38px; text-align:center; float:right; position:relative; right:-2px; top:-44px;}
.box .wbk .srk .button input{cursor: pointer;}
.box .wbk .srk .button .tj-button{width:100px; height:38px; font:15px/38px "微软雅黑"; color:#ffffff; border:none; background:#48b913;}
.box .add{width:300px; height:112px; position:relative; margin:30px 0 0 282px;}
.box .add .text{width:216px; height:30px; border:2px solid #ccd4d9; margin-bottom:5px; font:13px/30px "微软雅黑"; color:#bba9bb;}
.box .add .texts{width:216px; height:30px; border:none; background:none; font:13px/30px "微软雅黑"; color:#bba9a9; text-indent:6px;}
.box .add .label{width:79px; height:112px; position:absolute; top:; right:; font:13px/37px "微软雅黑"; color:#777777;}
.box .add .label .lab{text-indent:10px;}
JS:
function change(x){document.getElementById(x).value = '';}
function bold(){
var obj = document.getElementById('txtarea');
if(obj.style.fontWeight == ''|| obj.style.fontWeight == 'normal'){obj.style.fontWeight = 'bold';}
else{obj.style.fontWeight = 'normal';}
}
function italic(){
var obj = document.getElementById('txtarea');
if(obj.style.fontStyle == '' || obj.style.fontStyle == 'normal'){obj.style.fontStyle = 'italic';}
else{obj.style.fontStyle = 'normal';}
}
function linetrough(){
var obj = document.getElementById('txtarea');
var value = obj.style.textDecoration;
if(value == '' || value == 'none' || value == 'underline'){if(value == 'underline'){obj.style.textDecoration = 'line-through underline'}else{obj.style.textDecoration = 'line-through'};}
else{obj.style.textDecoration = 'none';}
}
function underline(){
var obj = document.getElementById('txtarea');
var value = obj.style.textDecoration;
if(value == '' || value == 'none' || value == 'line-through'){if(value == 'line-through'){obj.style.textDecoration = 'line-through underline'}else{obj.style.textDecoration = 'underline'};}
else{obj.style.textDecoration = 'none';}
}
function Quotes(){
var total = Handletext('txtarea').slice();
if(total.length == 1){total[0].innerHTML = total[0].innerHTML + '\"' + '\"' ;return;}
if(total.length > 1){total[0].innerHTML = total[1] + '\"' + total[2] + "\"" + total[3]}
}
function Handletext(x){
var obj = document.getElementById(x);
var selecttext = obj.innerHTML.substring(obj.selectionStart,obj.selectionEnd);
if(selecttext.length == 0){ var total = [obj];return total;}
var start = obj.innerHTML.indexOf(selecttext);
var end = start + selecttext.length;
var textlength = obj.innerHTML.length;
var starttext;
var endtext;
if(start > 0){starttext = obj.innerHTML.substring(0,start);}
else{starttext = '';}
if(textlength > end){endtext = obj.innerHTML.substring(end,obj.innerHTML.length);}
else{endtext = '';}
var total = [obj,starttext,selecttext,endtext];
return (total);
}
很多地方都可以优化得更好,这里由于是之前的代码,就不进行优化了,代码质量随着学习的深入会越来越精简。
【原生JS】评论编辑器 文本操作的更多相关文章
- [笔记]原生JS实现的DOM操作笔记
原生JS实现的DOM一系列操作参考: 原生JavaScript封装DOM库 siblings: 原生JS-查找相邻的元素-siblings方法的实现 addClass,removeClass,hasC ...
- 原生js封装table表格操作,获取任意行列td,任意单行单列方法
V1.001更新增加findTable-min.js 本次更新,优化了代码性能方面,增加了部分新功能,可以获取多个table表格批量操作. 考虑到本人后面的项目中可能涉及到大量的表格操作,提前先封了 ...
- 原生JS的DOM节点操作
DOM(Document Object Model/文档对象模型)是针对HTML和XML文档的一个API.DOM节点树:在文档中出现的空格.回车.标签.注释.文本.doctype.标签等都属于DOM节 ...
- 原生JS的对象常用操作总结
前端时间写了篇怎么判断js对象相等的文章,一直在期待大神给点消息,无奈一直杳无音讯,还是自己写个函数来进行判断,下面总结一些常用的对象操作的方法. 咋们来个先抑后扬的方式,先放出几个基本的 ...
- 原生js动态创建文本内容的几种方式
1.通过CreateTextNode文本节点 首先创建该元素(元素节点),然后向一个已存在的元素追加该文本节点 <!DOCTYPE html> <html> <body& ...
- 原生js实现复制文本到粘贴板
项目中经常会遇到点击按钮复制订单号.订单id等内容到粘贴板中的需求.可是通常我们都是用Ctrl + c或右击复制的,别操心,js也是有复制命令的,那就是document.execCommand('co ...
- 原生JS和jQuery操作DOM的区别小结
一.Js原生对象和jQuery实例对象的相互转化: (1).原生JS对象转JQ对象: $(DOM对象); (2). JQ对象转原生JS对象: $(DOM对象).get(index); //注意区分eq ...
- 原生js与jquery操作iframe
1 原生js获取iframe的window对象 //方法1 document.getElementById('iframeId').contentWindow; //方法2 window.frame ...
- 原生js 样式的操作整理
内联样式的获取 function getStyle(obj,attr){//简单的获取内联样式 return obj.currentStyle?obj.currentStyle[attr]:obj.g ...
随机推荐
- vue学习之组件(component)(一)
组件 (Component) 是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能.在有些情况下, ...
- xml-apis.jar getTextContent() jar包冲突解决(getTextContent()方法无法找到)
1.引用包: import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList; 2.方法中应用: ...
- Unity3D研究院之为什么Inspector视图中脚本前面的勾选框没了
我一个同事刚问我为啥有时候脚本的勾选项没有了?有时候不想让某条脚本执行,可以直接在编辑器中点掉勾选按钮即可.如下图所示 以前我也遇到过这个问题,但是一直都没怎么注意,因为一般情况下也用不到.今天刚好有 ...
- StringUtils常用方式留存
StringUtils是org.apache.commons.lang下的一个工具包.主要用途从名字可以看出是针对于String的一些操作工具,里面包含的方法非常多,英语水平尚可以的人可以前往它的官方 ...
- LDAP Authentication Handler
Including the Handler In the pom.xml file for your CAS Maven2 WAR Overlay, add the following depende ...
- JQuery--mouseover()与moseout()的区别
mouseover()与mouseout()区别 普通鼠标移入移出事件 语法: mouseover()/mouseout() 功能: 当鼠标移入/移出到添加事件的元素或其子元素的时候,都会被触发!!m ...
- kubernetes1.5新特性跟踪(续)
Kubernetes发布历史回顾 Kubernetes 1.0 - 2015年7月发布 Kubernetes 1.1 - 2015年11月发布 Kubernetes 1.2 - 2016年3月发布 K ...
- blogbeta1
//html <!DOCTYPE html> blog 身高:170 体重:230 座右铭 再给我吃一口 关于我 微信 微博 标签 SM SP 重金求爹 2019/11/16 本人找爹,带 ...
- Directx11教程(65) 渲染到纹理
原文:Directx11教程(65) 渲染到纹理 通常情况下,我们的render target都是后缓冲,但也可以把render target设置为一个2d 纹理,然后再通过贴图的方式,把这个 ...
- java方法重写规则 重载
方法的重写规则 参数列表必须完全与被重写方法的相同: 返回类型必须完全与被重写方法的返回类型相同: 访问权限不能比父类中被重写的方法的访问权限更低.例如:如果父类的一个方法被声明为public,那么在 ...