假设我们有以下内容:
node[1].name = "apple";
node[1].color = "red";
node[2].name = "cherry";
node[2].color = "red";
node[3].name = "apple";
node[3].color = "green";
node[4].name = "orange";
node[4].color = "orange;
Run Code Online (Sandbox Code Playgroud)
如果我使用jQuery.unique(节点),我将获得所有原始节点,因为它们都有不同的名称或颜色.我想要做的只是获取具有唯一名称的节点,该名称应返回
node[1] (apple)
node[2] (cherry)
node[4] (orange)
Run Code Online (Sandbox Code Playgroud)
它不应该返回3,因为它是相同的水果,即使我们有绿色和红色的苹果.
由于将javascript DOM方法放在html页面的底部(在<body>之后)比使用jQuery"ready"事件要快得多,所以我们不应该通过这样做来强制它:
$('document').trigger('ready');
Run Code Online (Sandbox Code Playgroud)
...身体标签后?我还没有尝试过这个,但它应该加快速度.还是我错过了什么?
我需要使用regExp提取文件的完整路径
mydomain.com/path/to/file/myfile.html -> mydomain.com/path/to/file/
/mypath/file.txt -> /mypath/
任何人?
我知道jQuery不会缓存元素集合,f.ex调用:
$('.myclass').html('hello');
$('.myclass').html('bye');
Run Code Online (Sandbox Code Playgroud)
将使jQuery两次爬上DOM.
但缓存的DOM节点怎么样?
var elems = document.querySelectorAll('.myclass');
$(elems).html('hello');
$(elems).html('bye');
Run Code Online (Sandbox Code Playgroud)
jQuery会在内部缓存这些内容,还是会像第一个例子一样缓慢?
澄清一下:jQuery 是否会在内部保留对内容的引用elems和缓存,$(elems)以便$()每次都不必应用相同的包装器?
就像是:
cache = {}
constructor = function(collection)
if collection in cache
return cache[collection]
else construct(collection)
Run Code Online (Sandbox Code Playgroud) 我想每20秒运行一些代码.这很好.但是我想setInterval在第一次遇到时运行,然后开始计时(而不是先做时间).
显然,我可以这样做:
myFunction();
setInterval(myFunction, 20000);
Run Code Online (Sandbox Code Playgroud)
但我觉得有点不雅.我宁愿做类似的事情
setInterval(myFunction, 20000, { waitBeforeFirstRun: false });
Run Code Online (Sandbox Code Playgroud)
这样的设置是否存在setInterval?
如何将画布图像的RGB色彩空间限制为特定的颜色数组?F.ex:
var colors = ['#aaffee','#cc44cc','#00cc55','#0000aa'];
var rgb = [230,111,90];
// match rgb with the colors and return the closest match
Run Code Online (Sandbox Code Playgroud)
当我遍历画布图像数据中的像素时,我需要从该数组获得最接近的匹配.是否有一个聪明的功能可以做到这一点?
我想$.fn.modal在Bootstrap/jQuery中进行扩展,这样我的扩展版本可以在通过传递额外选项初始化模态之前"做事".我试过这个,就像我做一些时间来扩展jQuery原型:
var modal = $.fn.modal;
$.fn.modal = function() {
this.each(function() {
// do stuff
});
modal.apply(this, arguments);
}
$('#modal').modal(); // fails
Run Code Online (Sandbox Code Playgroud)
上面的例子不起作用,虽然相同的模式适用于许多其他非bootstrap jQuery原型.
这是一个演示它的小提琴:http://jsfiddle.net/eTWSb/(只是评论覆盖的东西,看它工作).
当你想要添加自定义功能时,我的印象是克隆一个函数然后将其称为"超级"是一个可行的选择.
有什么建议?
我正在尝试为React中的子项设置一些内联样式.我有这个实际上有效,有点:
var App = React.createClass({
render: function() {
var children = React.Children.map(this.props.children, function(child, i) {
child.props.style.width = '100px'
})
return (
<div>{children}</div>
)
}
})
Run Code Online (Sandbox Code Playgroud)
但是,如果我在状态发生变化时尝试更改样式,样式将保持不变.在尝试这样的事情时你可以很容易地看到这个:(小提琴:http://jsfiddle.net/9UvWL/)
var World = React.createClass({
getInitialState: function() {
return { width: 0 }
},
componentDidMount: function() {
this.setState({ width: 100 })
},
componentDidUpdate: function() {
console.log(this.getDOMNode().innerHTML);
},
render: function() {
var width
var children = React.Children.map(this.props.children, function(child, i) {
width = i*this.state.width
console.log('Setting width: '+width);
child.props.style = {width: (i*this.state.width)+'px'}
return child …Run Code Online (Sandbox Code Playgroud) 对此进行排序的最佳方法是什么:
{
abc: {
string: 'lorem',
date: 2
},
enc: {
string: 'ipsum',
date: 1
}
}
Run Code Online (Sandbox Code Playgroud)
进入这个:
[{
id: 'enc',
string: 'ipsum',
date: 1
},
{
id: 'abc',
string: 'lorem',
date: 2
}]
Run Code Online (Sandbox Code Playgroud)
我需要一个按日期(数字)排序的数组和一个平面对象。
我的对象/功能范围和对某些事物的一般理解有问题.我正在调用doc中的jQuery函数,如下所示:
$("#interactiveQA .actionTile").on('click',function(){
$(this).activeTiles("init");
});
Run Code Online (Sandbox Code Playgroud)
然后我在这里创建脚本:
(function($) {
var methods = {
init: function() {
var tileClicked = $(this).attr('id');
}
};
$.fn.activeTiles = function(method) {
// Method calling logic
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.slider');
}
};
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
这是我想要找到的.尽可能在技术上,你能解释为什么var tileClicked = $(this)attr('id')在当前位置不起作用吗?您能否请详细解释您将采取哪些不同的做法或最佳做法等?
我真的不明白如何在文档就绪时正确调用Javascript函数.我的目标是根据浏览器窗口的高度调整div的大小,并从SO上的另一个答案中获取此代码.我究竟做错了什么?
function resizeElementHeight(element) {
var height = 0;
var body = window.document.body;
if (window.innerHeight) {
height = window.innerHeight;
} else if (body.parentElement.clientHeight) {
height = body.parentElement.clientHeight;
} else if (body && body.clientHeight) {
height = body.clientHeight;
}
element.style.height = ((height - element.offsetTop) + "px");
}
$(document).ready(resizeElementHeight($('#global_image')));
Run Code Online (Sandbox Code Playgroud)
我试图调用的元素具有id"global_image",但我得到"不能设置未定义的属性高度",并且我觉得如何为函数提供正确的元素感到迷茫.谢谢你的帮助!
javascript ×11
jquery ×5
arrays ×2
caching ×1
canvas ×1
colors ×1
domready ×1
events ×1
extend ×1
modal-dialog ×1
object ×1
performance ×1
plugins ×1
reactjs ×1
regex ×1
rgb ×1
setinterval ×1
sorting ×1
unique ×1