Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery—单击链接,将所有类从一个元素复制到另一个元素_Jquery_Class_Copy_Click_Element - Fatal编程技术网

jQuery—单击链接,将所有类从一个元素复制到另一个元素

jQuery—单击链接,将所有类从一个元素复制到另一个元素,jquery,class,copy,click,element,Jquery,Class,Copy,Click,Element,(参见JSFIDLE示例) 单击“.link_to_rule_them_all”后,我希望将所有的“.link_to_rule_them_all”类复制到#框中,并在每次单击之间清除“#框” 下面是我的示例代码和说明 正如你所看到的,我对这个的js一无所知。。有什么想法吗?呃 $('.link_to_rule_them_all').bind('click', function(e) { e.preventDefault(); $('#box').attr('class', ($(

(参见JSFIDLE示例)

单击“.link_to_rule_them_all”后,我希望将所有的“.link_to_rule_them_all”类复制到#框中,并在每次单击之间清除“#框”

下面是我的示例代码和说明

正如你所看到的,我对这个的js一无所知。。有什么想法吗?呃

$('.link_to_rule_them_all').bind('click', function(e) {
    e.preventDefault();
    $('#box').attr('class', ($('span', $(this)).attr('class')));
});
现场示例:

使用以下内容:

<a class="link_to_rule_them_all" href="javascript://">

$('.link_to_rule_them_all').click(function() {
 $('#box').attr('class',$(this).attr('class'))    
})

$('.link_至_rule_them_all')。单击(函数(){
$('#box').attr('class',$(this.attr('class'))
})

使用null href而不是散列是正确的形式。

首先,您可以将侦听器直接添加到span中,如下所示:

$('span').click(function(e) {
然后可以将class属性添加到DIV中

$('#box').append($(e.target).attr('class'));
好吧,让我们试试这个

$('.link_to_rule_them_all').click(function(e) {
//if you wanna attribute the span classes to the #box as classes
var box = $('#box')
box.removeClass();
box.addClass($(this).children('span').attr('class'));
})
如果要将span类添加为文本:

$('.link_to_rule_them_all').click(function(e) {
var box = $('#box');
box.text();
box.append($(this).children('span').attr('class'));
)}

是的,我知道,.click只是bind的一个包装,我想对不起,我不确定它是否重要,但这里有代码的更新。。应该有多个链接和一个box元素。。问题似乎是它永远不会清除中间的类。。它只是不断地增加。。明白我的意思吗?是的,很抱歉删除了我的帖子…它只是一个包装,但是如果你最小化代码,你可以节省用户加载时间…这些小细节会为你节省几千字节。不过我把链接放在那里是有原因的。。我还更新了第一篇帖子,说每次点击之间都应该清除“#box”元素。。否则它只会不断增加。。对吧?对不起,我不是故意粗鲁的。我更新了其他内容,我不确定它是否合适,但您还有2个选项。如果HREF指向某个地方,您将被带到另一个页面,因此您将无法看到此页面的运行情况。