小编brk*_*brk的帖子

这是因为Javascript beign单线程吗?

我遇到了这样一个片段

(function(){
    for(var i=0;i<3;i++){
    setTimeout(function(){
    console.log(i)
   })
  }
}())
Run Code Online (Sandbox Code Playgroud)

我希望它记录1,2 ....而不是它记录3.不确定这是因为js beign单线程,并且只在完成循环后查找队列.

工作复印

javascript

4
推荐指数
1
解决办法
85
查看次数

什么是#auto属性,以及为什么需要它

我正在尝试学习角度材料2,并#auto在自动完成中遇到了这个属性.我理解auto可以替换为任何文本,但为什么#之前需要一个这里auto有什么名称?

<md-input-container>
  <input mdInput placeholder="State" [mdAutocomplete]="auto" [formControl]="stateCtrl">
</md-input-container>

<md-autocomplete #auto="mdAutocomplete">
               ^^^^ what is name of this property
  <md-option *ngFor="let state of filteredStates | async" [value]="state">
    {{ state }}
  </md-option>
</md-autocomplete>
Run Code Online (Sandbox Code Playgroud)

javascript angular2-forms angular

4
推荐指数
1
解决办法
3846
查看次数

为什么 Object.assign() 拆分字符串并创建具有多个键的对象?

如果将 astring literal传递给Object.assign(),它将创建一个object具有多个keys这样的示例:

var v1 = 'myTest';
var obj = Object.assign({}, v1);
console.log(obj);
Run Code Online (Sandbox Code Playgroud)

这背后的原因是什么,而不是返回{0:'myTest'}

javascript

4
推荐指数
1
解决办法
879
查看次数

为什么我们在XMLHttpRequest中编写send()之前编写onload()函数

我是XMLHttpRequest的新手.我不明白为什么我们在send()函数之前编写onload()函数.onload()函数处理我们收到的响应和send()函数向服务器发送请求.所以onload()必须按照我的理解在send()函数之后编写.有人可以帮助理解这一点.

var xmlhttp = new XMLHttpRequest(),
  method = 'GET',
  url = 'https://developer.mozilla.org/';

xmlhttp.open(method, url, true);
xmlhttp.onload = function () {
  // Do something with the retrieved data
};
xmlhttp.send();
Run Code Online (Sandbox Code Playgroud)

javascript xmlhttprequest

3
推荐指数
1
解决办法
4025
查看次数

.is("可见")和.is(":可见")的工作方式不同

我正在使用jquery可见选择器来检查子元素是否可见.但令人惊讶的是.is("可见")和.is(":visible")在使用css可见性时显示不同的结果:隐藏属性.当我正在使用.is('可见')它警告错误并且使用.is(":visible")它是警告真实的.但是在将css属性更改为display:none时,结果是一致的.这是代码.

HTML

<div id="parentEle">
    I have hidden span
    <span class="hiddenContent">
        I am hiddenContent
    </span>
</div>
<button type="button" onclick="_checkChild()">Check Child</button>
Run Code Online (Sandbox Code Playgroud)

JS

function _checkChild(){
    var x= false;
    x =$("#parentEle").children(".hiddenContent").is(":visible");
    alert(x);
}
Run Code Online (Sandbox Code Playgroud)

CSS

.hiddenContent{
   visibility:hidden
}
Run Code Online (Sandbox Code Playgroud)

的jsfiddle

能帮助您理解这种差异吗?谢谢

javascript css jquery

2
推荐指数
1
解决办法
831
查看次数

在JavaScript中将数组转换为格式化字符串

我有一个数组元素

    [{
    "x": 326,
    "y": 176
}, {
    "x": 244,
    "y": 300
}, {
    "x": 189,
    "y": 420
}, {
    "x": 154,
    "y": 546
}, {
    "x": 139,
    "y": 679
}, {
    "x": 152,
    "y": 827
}, {
    "x": 183,
    "y": 954
}, {
    "x": 230,
    "y": 1088
}, {
    "x": 291,
    "y": 1217
}, {
    "x": 365,
    "y": 1333
}, {
    "x": 446,
    "y": 1417
}, {
    "x": 554,
    "y": 1469
}]
Run Code Online (Sandbox Code Playgroud)

我想将此转换为字符串

"{326,176},{244,300},{189,420},{154,546},{139,679},{152,827},{183,954},  {230,1088},{291,1217},{365,1333},{446,1417},{554,1469}"
Run Code Online (Sandbox Code Playgroud)

我们怎样才能在javascript中实现这一点..在此先感谢..

javascript arrays

2
推荐指数
1
解决办法
1803
查看次数

变量===常量和常量===变量之间有什么区别

我有以下代码片段按预期工作.哪里x是变量

var myVariable = (x === 'A' || x=== 'B') ? 'sui' : 'pai';
Run Code Online (Sandbox Code Playgroud)

但是闭包编译器正在将其转换为

var myVariable = ('A' === x || 'B'=== x) ? 'sui' : 'pai';
Run Code Online (Sandbox Code Playgroud)

这也有效.请你告诉我第二个片段比第一个片段更好.谢谢.

javascript

2
推荐指数
1
解决办法
76
查看次数

虽然属性值正在更新,但复选框未检查

我有一个复选框,我想每隔2秒使用setInterval检查/取消选中.

但它只被检查/取消选中一次.

此外,当我检查DOM时,我可以看到它的值和checked属性正在更新,但它没有反映在视图中.

这是我的代码

HTML

<input type="checkbox" name="paid" id="paid-change" value = "1">
Run Code Online (Sandbox Code Playgroud)

JS

var getCheckBox = $("#paid-change");

function check(){
 getCheckBox.attr('checked',true);
 getCheckBox.val("1")
}
function uncheck(){
getCheckBox.attr('checked',false);
 getCheckBox.val("0")
}

// check/uncheck in every 2 seconds
setInterval(function(){
 switch (getCheckBox.val()){ //get checkbox value
 case "0":
     check() // if value is 0 check it
     break;
 case "1":
     uncheck() //uncheck it
     break;
}

},2000)
Run Code Online (Sandbox Code Playgroud)

这是一个jsfiddle.

检查元素,属性是更新但不反映在视图上.

知道我犯错误的地方吗?

javascript jquery

1
推荐指数
1
解决办法
46
查看次数

为什么 forEach 数组方法不是遍历 document.getElementsByClassName 的函数

为了获得匹配的元素并对其进行循环,我使用了 querySelectorAll&forEach数组方法。根据querySelectorAll将返回nodelist

我也可以使用getElementsByClassNameby get 匹配的元素,但不能直接使用forEach. 但是这个链接告诉我们它也返回一个节点列表对象

在使用forEach它返回时getElementsByClassName会引发错误,forEach is not a function

当两个&都forEach返回时getElementsByClassName,您能否让我知道为什么不能直接用于返回getElementsByClassNamequerySelectorAllnodelist

javascript

1
推荐指数
1
解决办法
932
查看次数

Not able to understand why function returning undefined

I am creating a function to remove the underscore if it is the first character of the string.

The function converts the input to uppercase and get the first character and after a validation if requires it removes the first character. Then again calling the same function with modified input to re validate. If it pass the condition I am returning the modified word from the else.But somehow it is returning undefined

let word = '__Hello_World';

function removeFirstUnderscore(ipWord) { …
Run Code Online (Sandbox Code Playgroud)

javascript

-2
推荐指数
1
解决办法
38
查看次数