是否可以调用在函数外部设置的相同名称变量?
var a = $(window).width(); // I want to call this variable
if(!$.isFunction(p)){
var a = $(window).height(); // Not this one
alert(a);
}
Run Code Online (Sandbox Code Playgroud)
我想只编写一次数组元素.如果我想写3它应该只写一次.是否有可能使用数组来实现它.
我的代码
var arr = [ 1,1,1,1,1,1,1,1,3,3,3,3,3,3,4,4,4,4];
arr = $.grep(arr, function( x, y ) {
return ( x !== 1);
});
$( "p>span" ).text( arr.join( ", " ) );
Run Code Online (Sandbox Code Playgroud)
演示 ;
我想问一下使用$()或不使用的差异$().在两种情况下,它实际上是有效的.有什么不同我真的不明白.
jQuery的
var harf = $('<p>Hello there</p>').appendTo('body');
$(harf).on('click', function(){
alert('harf');
});
Run Code Online (Sandbox Code Playgroud)
//
var harf = $('<p>Hello there</p>').appendTo('body');
harf.on('click', function(){
alert('harf');
});
Run Code Online (Sandbox Code Playgroud) 我想用jQuery隐藏我的第二个图像,.hide()但在某些情况下该方法不起作用.
这是我的代码:
jQuery的
var one = $('ul li > img')[0];
var two = $('ul li img')[1];
var three = $('ul li img')[2];
$(two).on('click', function(){
alert('working');
});
two.hide();
Run Code Online (Sandbox Code Playgroud)
HTML
<ul>
<li><img src="http://img.pixland.uz/u13301f281573m.jpg" alt=""></li>
<li><img src="http://img.pixland.uz/u13301f281573m.jpg" alt=""></li>
<li><img src="http://img.pixland.uz/u13301f281573m.jpg" alt=""></li>
</ul>
Run Code Online (Sandbox Code Playgroud)