我想找到具有id的子div的索引'whereami'.
<div id="parent">
<div></div>
<div></div>
<div id="whereami"></div>
<div></div>
</div>
Run Code Online (Sandbox Code Playgroud)
目前我正在使用此函数来查找子项的索引.
function findRow(node){
var i=1;
while(node.previousSibling){
node = node.previousSibling;
if(node.nodeType === 1){
i++;
}
}
return i; //Returns 3
}
var node = document.getElementById('whereami'); //div node to find
var index = findRow(node);
Run Code Online (Sandbox Code Playgroud)
小提琴:http://jsfiddle.net/grantk/F7JpH/2/
问题
当有数千个div节点时,while循环必须遍历每个div来计算它们.这可能需要一段时间.
有没有更快的方法来解决这个问题?
*请注意,id将更改为不同的div节点,因此需要能够重新计算.
let在javascript中使用关键字有哪些选项?看起来它真的很有用.
我见过traceur,但我想知道是否有其他选项,所以我不必通过它运行整个项目.
甚至可以使用某种类型的polyfill或库.或者我基本上必须等到所有旧浏览器消失才能原生使用它...
我有一个对象数组,如下所示.
var bundles = [
{
src: 'js/my-component/*.js',
bundleName: 'my-component.js'
},
{
src: 'js/my-other-component/*.js',
bundleName: 'my-other-component.js'
}
]
Run Code Online (Sandbox Code Playgroud)
我希望gulp任务处理/连接数组中的每个条目,但它似乎不起作用.
gulp.task('bundlejs', function(){
return bundles.forEach(function(obj){
return gulp.src(obj.src)
.pipe(concat(obj.bundleName))
.pipe(gulp.dest('js/_bundles'))
});
});
Run Code Online (Sandbox Code Playgroud) 我有一个托管在s3上的图像,当放置在图像标签内时,从图像到横向翻转.
Chrome在页面中将其显示为水平:http: //imgur.com/kJNzNQG,PgJPUsm#0
但是当我在新标签页中打开时,它显示为肖像. http://imgur.com/kJNzNQG,PgJPUsm#1
结果:
我不能直接链接到图像,因为它们是用户的照片.
有没有人遇到过这个?
我有一个任务来限制字母值的文本框.只有浮点值才有可能.在十进制之后我们必须写出两位数.我这样做但它在Mozilla Firefox中不起作用.我怎么解决这个问题?
我的剧本是
$(function () {
$('#name').bind('paste', function () {
var self = this;
setTimeout(function () {
if (!/^[a-zA-Z]+$/.test($(self).val())) $(self).val('');
}, 0);
});
$('#salary').bind('paste', function () {
var self = this;
setTimeout(function () {
if (!/^\d*(\.\d{1,2})+$/.test($(self).val())) $(self).val('');
}, 0);
});
$('.decimal').keypress(function (e) {
var character = String.fromCharCode(e.keyCode)
var newValue = this.value + character;
if (isNaN(newValue) || hasDecimalPlace(newValue, 3)) {
e.preventDefault();
return false;
}
});
function hasDecimalPlace(value, x) {
var pointIndex = value.indexOf('.');
return pointIndex >= 0 && pointIndex < value.length …Run Code Online (Sandbox Code Playgroud) javascript ×4
html ×3
amazon-s3 ×1
browser ×1
dom ×1
gulp ×1
gulp-concat ×1
image ×1
jquery ×1
polyfills ×1