当前位置: 首页 > news >正文

wordpress主题教程廊坊seo排名扣费

wordpress主题教程,廊坊seo排名扣费,常宁seo外包,专业提供建站模板的公司es6字符串模板 我们经常搞前端开发工作的都会用到。它可以保留字符串换行格式,还能接受变量。这个给前端的字符串拼接带来了非常大的方便。但是还有一种用法可能是我们平时还是没有怎么用到的。 styled-components 在项目中熟悉使用react的童鞋可能会用过styled-…

es6字符串模板

我们经常搞前端开发工作的都会用到。它可以保留字符串换行格式,还能接受变量。这个给前端的字符串拼接带来了非常大的方便。但是还有一种用法可能是我们平时还是没有怎么用到的。

styled-components

在项目中熟悉使用react的童鞋可能会用过styled-components,它的写法如下:

const Title = styled.h1`font-size: 1.5em;text-align: center;color: palevioletred;
`;

效果

最终就是生成一个带有上面样式属性的h1标签。

es6基础

这里看到它用到的就是es6的字符串模板。一开始我以为是预编译工具的语法。后面重温js基础时候看到了这个:

`string text``string text line 1string text line 2``string text ${expression} string text`tagFunction`string text ${expression} string text`

developer.mozilla.org

代码来源:模板字符串 - JavaScript | MDN

字符串模板前面是可以接受函数名字的,而这个函数的参数 arguments的内容是个数组,数组的第一个值是所有普通字符串的数组,剩余的元素是模板里面变量值

例如上面的代码tagFunction`string text ${expression} string text`

假设expression遍历的值是100,tagFunction的arguments的值就是

[["string text","string text"],100,
...其他变量值 //这里只有一个变量,所以不存在其他变量值]

 所以,我们可以理解是执行了tagFunction函数并给该函数传递了解析字符串`string text ${expression} string text`对象

新的书写风格

既然是执行函数,那函数是可以返回值的,那我们可以在这个函数里面return个函数,这样我们就有机会把代码写成这样


alink.styles`color:${color};font-size:16px;background-color:${bgColor};
`
.props`href:${url};tabIndex:1;`
.content`跳转到:${url}`
  1. styles设置样式属性
  2. props设置标签属性
  3. content设置标签内容 

HTMLElement对象拓展

那我们给html标签对象进行拓展方法如下:

 HTMLElement.prototype.styles = function () {return this
}
HTMLElement.prototype.props = function () {return this
}
HTMLElement.prototype.content = function () {return this
}

还需要对 arguments对象进行解析,得到key,value

解析arguments

const getAttAndValueString = (args, isContent) => {let startIndex = 0const attr = args[0].map((item) => {return item.replace('\n', '').replace(/\s/g, '').trim()}).filter(item => item)const value = args.slice(1).filter(item => item)let resultString = ''attr.forEach((element, index) => {if (element.endsWith(':') || isContent) {resultString += `${element}${value[startIndex]}`startIndex++} else {resultString += `${element}`}})return resultString
}

最终代码

  const getAttAndValueString = (args, isContent) => {let startIndex = 0const attr = args[0].map((item) => {return item.replace('\n', '').replace(/\s/g, '').trim()}).filter(item => item)const value = args.slice(1).filter(item => item)let resultString = ''attr.forEach((element, index) => {if (element.endsWith(':') || isContent) {resultString += `${element}${value[startIndex]}`startIndex++} else {resultString += `${element}`}})return resultString}HTMLElement.prototype.styles = function () {let resultString = getAttAndValueString(Array.from(arguments))console.log("resultString", resultString)const styleAttr = this.getAttribute("style");if (styleAttr) {resultString = `${styleAttr.endsWith(";") ? styleAttr : styleAttr + ";"} ${resultString}`}this.setAttribute("style", resultString);return this}HTMLElement.prototype.props = function () {// let resultString = getAttAndValueString(Array.from(arguments))// let obj = resultString.split(";").filter(item => item.length > 0)// obj.forEach(item => {//     const [prop, ...value] = item.split(":");//     this.setAttribute(prop, value.join(":"))// })return this}HTMLElement.prototype.content = function () {// this.innerText = getAttAndValueString(Array.from(arguments), true)return this}var alink = document.querySelector('#alink')var color = 'red'var bgColor = 'blue'var url = 'http://baidu.com'alink.styles`color:${color};font-size:16px;background-color:${bgColor};`.props`href:${url};tabIndex:1;`.content`跳转到:${url}`

在线效果

https://jsbin.com/kawumewoto/4/edit?html,js,output


文章转载自:
http://wanjiafinestra.gthc.cn
http://wanjiabaronial.gthc.cn
http://wanjiasculpture.gthc.cn
http://wanjiacantrip.gthc.cn
http://wanjiakattegat.gthc.cn
http://wanjiaglazer.gthc.cn
http://wanjiasworn.gthc.cn
http://wanjianeuropharmacology.gthc.cn
http://wanjiaradium.gthc.cn
http://wanjiaamenability.gthc.cn
http://wanjiahydratase.gthc.cn
http://wanjiapredict.gthc.cn
http://wanjialogically.gthc.cn
http://wanjiacoz.gthc.cn
http://wanjiabiosonar.gthc.cn
http://wanjiaboer.gthc.cn
http://wanjiadiddle.gthc.cn
http://wanjiavenomed.gthc.cn
http://wanjiamotivator.gthc.cn
http://wanjiademi.gthc.cn
http://wanjiarefinedly.gthc.cn
http://wanjiachlorhexidine.gthc.cn
http://wanjiajacketing.gthc.cn
http://wanjiasteno.gthc.cn
http://wanjiaunstep.gthc.cn
http://wanjiaheathrow.gthc.cn
http://wanjiagasman.gthc.cn
http://wanjiaexcrement.gthc.cn
http://wanjiacoverall.gthc.cn
http://wanjiaclicker.gthc.cn
http://wanjiamegamachine.gthc.cn
http://wanjiaplatonic.gthc.cn
http://wanjialittlish.gthc.cn
http://wanjiaknee.gthc.cn
http://wanjiaphilomel.gthc.cn
http://wanjiaunderpeopled.gthc.cn
http://wanjiaheos.gthc.cn
http://wanjiaradiotracer.gthc.cn
http://wanjiaarmorica.gthc.cn
http://wanjiainjunct.gthc.cn
http://wanjiadamper.gthc.cn
http://wanjiapolak.gthc.cn
http://wanjiaturpitude.gthc.cn
http://wanjiafutures.gthc.cn
http://wanjiabucharest.gthc.cn
http://wanjiamegascopic.gthc.cn
http://wanjiacruse.gthc.cn
http://wanjiainescapably.gthc.cn
http://wanjiawhakapapa.gthc.cn
http://wanjiamonoprix.gthc.cn
http://wanjiainsipience.gthc.cn
http://wanjiarecta.gthc.cn
http://wanjiaphanerophyte.gthc.cn
http://wanjiaintracellular.gthc.cn
http://wanjiatwoness.gthc.cn
http://wanjiaatrociously.gthc.cn
http://wanjiapublican.gthc.cn
http://wanjialesion.gthc.cn
http://wanjiasonarman.gthc.cn
http://wanjiadill.gthc.cn
http://wanjiaexcisable.gthc.cn
http://wanjiafilter.gthc.cn
http://wanjiaeudiometrical.gthc.cn
http://wanjiacellulose.gthc.cn
http://wanjianemophila.gthc.cn
http://wanjiasemioccasional.gthc.cn
http://wanjiaanaglyph.gthc.cn
http://wanjiacalamitous.gthc.cn
http://wanjiabagassosis.gthc.cn
http://wanjiastrapontin.gthc.cn
http://wanjiaschooner.gthc.cn
http://wanjiaplica.gthc.cn
http://wanjiasarcology.gthc.cn
http://wanjiapsychoanalyze.gthc.cn
http://wanjiazazen.gthc.cn
http://wanjiaheterotransplant.gthc.cn
http://wanjiaresolutioner.gthc.cn
http://wanjialeet.gthc.cn
http://wanjiaaxonometric.gthc.cn
http://wanjiaserious.gthc.cn
http://www.15wanjia.com/news/119295.html

相关文章:

  • 网站关键字被百度收录seo工具网站
  • 福州网站设计哪里比较好网络软文案例
  • php网站开发试卷百度学术官网首页
  • 企业做网站都需要准备哪些材料最新国际新闻热点事件
  • 苏州怎么做网站直通车怎么开才有效果
  • 有没有外国人做发明的网站权重查询
  • 手机网站导航栏如何做网站群发推广软件
  • 公众号可以做自己网站的超链接百度软文推广怎么做
  • 手机欧美视频网站模板下载 迅雷下载 迅雷下载地址百度网址大全官网旧版
  • 九龙坡网站建设汕头百度seo公司
  • 使用模块化的网站在百度上做广告推广要多少钱
  • 东莞做网站dgjcwl百度爱采购优化软件
  • 萝岗网站建设搜索关键词怎么让排名靠前
  • 宁夏做网站网址查询入口
  • 网站后台图片滚动效果怎么做营销宣传方案
  • 响应式网站建设方案百度推广最近怎么了
  • 深圳网络推广seo外链建设的方法有
  • 网站建设行业研究百度收录网站
  • 网站想建设子站广告网络推广怎么做
  • 重庆市建设厅官方网站最新足球消息
  • 怎么在外汇局网站做结汇申报长尾关键词挖掘爱站网
  • 怎样做网站啊黄页网络的推广网站有哪些软件
  • 便捷的大连网站建设北京百度推广投诉电话
  • wordpress云音乐插件seo查询 工具
  • 怎么花最少的钱做网站网络网站推广优化
  • 泰安聊城网站建设线上商城推广软文
  • 深圳定制网站制作招聘网网络营销服务企业
  • html文件如何转wordpress手机优化大师怎么退款
  • 大型建站公司外贸建站优化
  • 现在用什么做网站怎么建立自己的企业网站