小编ibr*_*rir的帖子

使用for循环创建div元素

这是一个非常简单的问题,但我不知道为什么它不起作用.我有一个包含4个项目的数组.我有一个容器,我想根据我的数组中的项目数插入一些div.我为此使用了for循环,但它只创建了一个div.它不应该创建4个相同的div元素吗?

这是代码:

count = new Array['1', '2', '3', '4'];
container = document.getElementById('items');

for (i = 0; i < count.length; i++) {
  container.innerHTML += '<div id="items"></div>';
}
Run Code Online (Sandbox Code Playgroud)
#items {
  width: 20px;
  height: 20px;
  background: gold;
}
Run Code Online (Sandbox Code Playgroud)
<div id="items"></div>
Run Code Online (Sandbox Code Playgroud)

这是链接 http://codepen.io/2bu/pen/jyxNbw

html javascript arrays

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

读取节点中的txt文件时出现意外标识符

我是node的初学者,我想通过读取txt文件创建一个列表数据结构,这是我读取该文件的代码。

        var fs = require("fs");
        var filename = require("../films.txt");

        fs.readFile(filename, 'utf8', function(err,data){
          var contents = data;
          var splitContents = contents.split("\n");
          var string = JSON.stringify(splitContents);
          console.log(string)
        });
Run Code Online (Sandbox Code Playgroud)

在此行下,我要阅读的文本文件是20部电影,文件名是films.txt。

    1. The Shawshank Redemption
    2. The Godfather
    3. The Godfather: Part II
    4. Pulp Fiction
    5. The Good, the Bad and the Ugly
    6. 12 Angry Men
    7. Schindler’s List
    8. The Dark Knight
    9. The Lord of the Rings: The Return of the King
    10. Fight Club
    11. Star Wars: Episode V …
Run Code Online (Sandbox Code Playgroud)

javascript node.js

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

为什么我得到"Uncaught ReferenceError",即使声明变量没有"var"关键字?

我遇到了以下代码段:

(function() {
    bar = 5;
    var bar = 10;
    console.log("value of bar inside the self invoking function = " + bar);
})();
console.log("value of bar out of function scope " + bar);
Run Code Online (Sandbox Code Playgroud)

当我执行上面的代码时,我得到:

"未捕获的ReferenceError"

为第二个控制台

javascript

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

如何在javascript函数中访问参数

这是一个javascript函数:

String.prototype.digit = function() {
  console.log(this); // 'this' contain the object 
  return false;
};
Run Code Online (Sandbox Code Playgroud)

如何'14'在调用函数时访问函数中的参数,如下所示:

'14'.digit();
Run Code Online (Sandbox Code Playgroud)

javascript function

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

大写数组中字符串的第一个字母

我正在尝试编写一个函数,只会将名字和姓氏的第一个字母大写......有关如何处理此问题的任何想法?

const namesHardest = ['emIly sMith', 'angeliNA Jolie', 'braD piTt'];

function capitalizeNames(peopleArray) {
    return peopleArray.toString().toLowerCase().split('').map(function(word) {
        return (word.charAt(0).toUpperCase() + word.slice(1));
    }).join(' ').split();
}


// set the resulting array to this variabble
const capitalizedNames = capitalizeNames(namesHardest);
capitalizedNames;
Run Code Online (Sandbox Code Playgroud)

javascript ecmascript-6

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

为什么下面的代码在输出中给出'undefined'

(function(){
    console.log(1);
    setTimeout (function(){console.log(2);},1000);
    setTimeout (function(){console.log(3);},0);
    console.log(4);
})();
Run Code Online (Sandbox Code Playgroud)

输出:

1
4
undefined
3
2
Run Code Online (Sandbox Code Playgroud)

为什么输出中有未定义的?

javascript settimeout

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

为什么我的函数没有列出数组的所有属性但是在ob上?

这是代码.我的函数不对该对象起作用,Array但对我ob用对象文字定义的对象起作用.

function all_properties(object) {
	var value = "[";
	for (var prop in object) {
		
			value += '"' + prop + '"' + ","
	}
	value = value.slice(0,-1);
	value += "]";
	return value;
}

var ob = {first: 1, second: function () {}, third: function () {}}
console.log(all_properties(ob))
console.log(all_properties(Array))
Run Code Online (Sandbox Code Playgroud)

javascript

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

JavaScript Array.shift无效

我一直在编写一个应该删除数组第一个索引的系统.控制台没有将数组从(即)"1,2,3,4,5"更改为"2,3,4,5",而是出现错误:"未捕获的TypeError:num.splice不是函数".我听说num.splice 不是一个函数,它是一个删除数组的第一个索引值的操作(或其他东西).我很困惑,当我使用w3Schools的示例代码时,控制台中没有输出错误.我不明白为什么会这样.

(我已经给出了整个代码以防它与语法问题有关)

function dCrypt() {
    var num = document.getElementById("demoin").value; // ex: a127
    var key = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
    var mod = 0;
    var out = 0;
    var prep = 0;
    var pre = 0;
    num.split("");
    mod = num[0];
    pre = key.indexOf(mod);
    num.splice(0,1);
    for (i=0;i <= pre;i++) {
        prep += 26;
    }
    out = Math.floor(num + pre);
    document.getElementById("demoout").innerHTML = out;
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

html javascript arrays

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

使用[this.length]向元素添加元素到数组

我试图通过使用当前数组长度作为下一个索引将项添加到数组.例如:

var arr = ["one","two","three"];
arr[this.length] = "four";
Run Code Online (Sandbox Code Playgroud)

但它只是用新元素替换了第一个元素,所以我得到了["four", "two", "three"].是this不是指阵列?

javascript arrays this

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

以下javascript代码的输出是什么?为什么?

我是JavaScript的新手.您能否解释下面的JavaScript代码的输出应该是什么?请尽可能详细解释原因.非常感谢.

var Foo = function( a ) { 
    function bar() {   
        return a; 
    }
    this.baz = function() {   
        return a; 
    };
};

Foo.prototype = {
    biz: function() {    
        return a; 
    }        
};

var f = new Foo( 7 );
f.bar(); 
f.baz(); 
f.biz(); 
Run Code Online (Sandbox Code Playgroud)

javascript function

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

标签 统计

javascript ×10

arrays ×3

function ×2

html ×2

ecmascript-6 ×1

node.js ×1

settimeout ×1

this ×1