jQuery-添加ID而不是类 [英] jQuery - Add ID instead of Class

查看:76
本文介绍了jQuery-添加ID而不是类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用当前的 jQuery :

$(function() {
    $('span .breadcrumb').each(function(){
        $('#nav').addClass($(this).text());
        $('#container').addClass($(this).text());
        $('.stretch_footer').addClass($(this).text())
        $('#footer').addClass($(this).text());
    });
});

它将面包屑中包含的文本应用于页面上的4个元素,这使我可以为其上的页面专门设置样式.

It applies the text held in the breadcrumb to 4 elements on the page, allowing me to style specifically to the page there on.

我想尝试添加一个ID而不是一个类,如何实现呢?

I'd like to try adding an ID instead of a class, how can I achieve this?

推荐答案

尝试一下:

$('element').attr('id', 'value');

它变成了

$(function() {
    $('span .breadcrumb').each(function(){
        $('#nav').attr('id', $(this).text());
        $('#container').attr('id', $(this).text());
        $('.stretch_footer').attr('id', $(this).text())
        $('#footer').attr('id', $(this).text());
    });
});

因此,您要更改/覆盖三个元素的ID,并向一个元素添加ID. 您可以根据需要进行修改...

So you are changing/overwriting the id of three elements and adding an id to one element. You can modify as per you needs...

这篇关于jQuery-添加ID而不是类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆