相关疑难解决方法(0)

合并jQuery对象

有可能将几个jQuery DOM对象合并到一个数组中并在所有数组上调用jQuery方法吗?

F.ex:

<button>one</button>
<h3>two</h3>

<script>

var btn = $('button');
var h3 = $('h3');

$([btn,h3]).hide();

</script>
Run Code Online (Sandbox Code Playgroud)

这不起作用.我知道我可以使用'button,h3'选择器,但在某些情况下我已经有几个jQuery DOM元素,我需要合并它们,所以我可以在所有这些上调用jQuery原型.

就像是:

$.merge([btn,h3]).hide();
Run Code Online (Sandbox Code Playgroud)

会工作.有任何想法吗?

更新:

解决了它.你可以这样做:

$.fn.add.call(btn,h3);
Run Code Online (Sandbox Code Playgroud)

我将接受这个add()建议,指出我正确的方向.

jquery

63
推荐指数
3
解决办法
3万
查看次数

如何将jquery对象分组以将css应用于一起?

假设我有一个接受多个jQuery对象的函数:

workWithThreeObjs($('.content'),
                  $('h1'),
                  $('input[type=button]'));
Run Code Online (Sandbox Code Playgroud)

我像这样定义我的函数:

function workWithThreeObjs(p, h1, buttons) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

在这个函数中,我想像这样对所有这些边框应用边框

$(p, h1, buttons).css(...);
Run Code Online (Sandbox Code Playgroud)

我怎么做?现在它只适用于p.

我的函数代码不控制传递给它的内容,因此我无法更改用于创建参数的选择器.我必须只对提供的jQuery对象进行操作.

jquery

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

如何结合jQuery find()和filter()的结果?

假设我编写了一个DOM片段:

var dom = $("
    <div id='root' class='abc'>
       <div id='child1' class='xxx'>no-match</div>
       <div id='child2' class='abc'>match!</div>
       <div id='child3' class='xxx'>no-match</div>
    </div>
");
Run Code Online (Sandbox Code Playgroud)

我想用'.abc'检索所有元素(即#root和#child2)

dom.find('.abc')将返回所有匹配的子元素(即只是#
child2 )dom.filter('.abc')将返回所有匹配的根元素(即只是#root)

我怎么能:
- 拨打一个同时找到#root和#child2的电话,或者
- 结合find()和filter()的结果

我已经看到其他关于"组合"结果的帖子,例如: 如何组合两个jQuery结果, 但这不是真正的"组合",而是"将更多结果附加到预先存在的结果"

我真正想要做的是类似于$.extend():

var one = dom.find('.abc');
var two = dom.filter('.abc');
var three = combine(one, two);
// or one.somefcn(two); // One is augmented
// or three = one.somefcn(two); // 
Run Code Online (Sandbox Code Playgroud)

任何信息/想法将不胜感激

jquery filter find

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

重复功能

在编写简单的东西时,我似乎重复了很多函数,有没有更好的方法呢?

  $('#bt1').click(function() {
      $('#txt1').show();
      $(this).css("background-image", "url(site_images/08/btn_over.png)");  
}); 

  $('#bt2').click(function() {
      $('#txt2').show();
      $(this).css("background-image", "url(site_images/08/btn_over.png)");  
}); 

  $('#bt3').click(function() {
      $('#txt3').show();
      $(this).css("background-image", "url(site_images/08/btn_over.png)");  
}); 

  $('#bt4').click(function() {
      $('#txt4').show();
      $(this).css("background-image", "url(site_images/08/btn_over.png)");  
}); 
Run Code Online (Sandbox Code Playgroud)

所以我不重复代码?

jquery

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

标签 统计

jquery ×4

filter ×1

find ×1