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

发布于 2024-10-26 21:30:18 字数 262 浏览 8 评论 0原文

(参见jsfiddle示例)

当单击“.link_to_rule_them_all”时,我想将所有“.link_to_rule_them_all span”类复制到#box中,并在每次单击之间清除“#box”。

我的示例代码和解释在这里http://jsfiddle.net/znCmq/2/

可以看到我不知道这个的js..有什么想法吗?呃..

(see jsfiddle example)

When the ".link_to_rule_them_all" is clicked i would like to copy all the '.link_to_rule_them_all span' classes into #box and clear the '#box' between every click.

my example code and the explanation is here http://jsfiddle.net/znCmq/2/

As you can see i have no idea about the js of this.. any ideas? eh..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撞了怀 2024-11-02 21:30:18
$('.link_to_rule_them_all').bind('click', function(e) {
    e.preventDefault();
    $('#box').attr('class', ($('span', $(this)).attr('class')));
});

实例:http://jsfiddle.net/moeishaa/3t33d/

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

live example : http://jsfiddle.net/moeishaa/3t33d/

鼻尖触碰 2024-11-02 21:30:18

使用这个:

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

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

使用 null href 而不是哈希是正确的形式。

Use this:

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

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

It's proper form to use a null href instead of a hash.

听不够的曲调 2024-11-02 21:30:18

首先,您可以像这样将侦听器直接添加到跨度中:

$('span').click(function(e) {

然后您可以将类属性添加到 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'));
})

如果您想将跨度类添加为文本:

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

First of all, you can add the listener directly to the span like this:

$('span').click(function(e) {

Then you can add the class attribute to the DIV

$('#box').append($(e.target).attr('class'));

Ok let's try this then

$('.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'));
})

If you wanna add the span class as TEXT:

$('.link_to_rule_them_all').click(function(e) {
var box = $('#box');
box.text();
box.append($(this).children('span').attr('class'));
)}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文