我用的是现在的jQuery:

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

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

我想try 添加一个ID而不是一个类,我该如何实现这一点?

推荐答案

试试这个:

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

So it becomes;

$(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.

Jquery相关问答推荐

如何限制select2中的字符?

如何在 jquery 中切换 attr()

如何滚动到AngularJS中的页面顶部?

将变量传递给jQuery AJAX成功回调中的函数

无法在被动事件侦听器中阻止默认值

谷歌图表重绘/zoom 窗口调整大小

$.each(selector) 和 $(selector).each() 有什么区别

jquery变量语法

我可以使用 jQuery 打开下拉列表吗

虽然未定义变量 - 等待

如何在 TypeScript 中获得 jQuery 自动完成功能?

如何使用 javascript 擦除所有内联样式并仅保留 css 样式表中指定的样式?

如何使用 jQuery 删除 cookie?

当 iframe 在 jQuery 中完成加载时,如何触发事件?

如何在一个元素上拥有多个数据绑定属性?

如何使用 javascript 展开和折叠

为什么要使用发布/订阅模式(在 JS/jQuery 中)?

Jquery Select 所有具有 $jquery.data() 的元素

HTMLCollection、NodeLists 和对象数组之间的区别

JQuery 判断 DOM 中的重复 id