简体   繁体   English

用于对象值设置程序的Javascript语法糖

[英]Javascript syntactic sugar for object value setter

Is there an easy way to do this in one line 有一种简单的方法可以在一行中做到这一点

( node.innerText || node.textContent ) = text
// node = DOM node  
// text = string

Obviously the above does not work. 显然以上方法不起作用。

只需使用node.innerText = node.textContent = text ,这是无害的。

一个人可以这样做:

node['textContent' in node? 'textContent' : 'innerText'] = text

首先,不太紧凑的尝试

node.innerText ? node.innerText = text : node.textContent = text

Use JQuery to shorten your syntax 使用JQuery缩短语法

use .text() method 使用.text()方法

so your code will be 所以你的代码将是

if($(selector).text() = text){
// your code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM