Javascript 如何更改我的<;的第一个类名;html>;让其他课程保持原样?

Javascript 如何更改我的<;的第一个类名;html>;让其他课程保持原样?,javascript,html,Javascript,Html,我有以下HTML: <html class="black abc"> or <html class="red def"> or <html class="red this is a test"> etc ... 我怎样才能把头等舱换成别的。 例如,我如何改变: <html class="black abc"> to <html class="red abc"> 到 给你: var tags = document.getEle

我有以下HTML:

<html class="black abc"> or 
<html class="red def"> or
<html class="red this is a test">
etc ...
我怎样才能把头等舱换成别的。 例如,我如何改变:

<html class="black abc">

to

<html class="red abc">
给你:

var tags = document.getElementsByTagName('html')[0];
var classList = tags.className.split(' '); //Converting class string to an array
classList[0] = 'red'; // Changing the first class
tags.className = classList.join(' '); // Adding updated class list
给你:

var tags = document.getElementsByTagName('html')[0];
var classList = tags.className.split(' '); //Converting class string to an array
classList[0] = 'red'; // Changing the first class
tags.className = classList.join(' '); // Adding updated class list

我想您正在寻找这个(只需要更改任何html元素的class属性)

您也可以使用JQuery来实现这一点

$("ID_OF_YOUR_ELEMENT").removeClass('PREVIOUS_CLASS').addClass('NEW_CLASS');

还有其他选项可用于执行此操作,JQuery支持

我想您正在寻找它(只需要更改任何html元素的class属性)

您也可以使用JQuery来实现这一点

$("ID_OF_YOUR_ELEMENT").removeClass('PREVIOUS_CLASS').addClass('NEW_CLASS');
还有其他选项可用于执行此操作,JQuery支持使用classList

删除类

var elem = document.getElementsByTagName('html')[0].classList.remove("black");
var elem = document.getElementsByTagName('html')[0].classList.add("red");
添加类

var elem = document.getElementsByTagName('html')[0].classList.remove("black");
var elem = document.getElementsByTagName('html')[0].classList.add("red");
使用类列表

删除类

var elem = document.getElementsByTagName('html')[0].classList.remove("black");
var elem = document.getElementsByTagName('html')[0].classList.add("red");
添加类

var elem = document.getElementsByTagName('html')[0].classList.remove("black");
var elem = document.getElementsByTagName('html')[0].classList.add("red");

-部分中定义一个函数,如下所示:

function class_replace_with(new_classname)
{
    var color1=html.className.split(' ')[0];
    html.className=html.className.substring(0, html.className.indexOf(' ')) + new_classname + html.className.substring(html.className.indexOf(' '));
}
因此,出于您的特定目的,将函数调用为:

class_replace_with('red');// as you have used red as the replacement for black.

-部分中定义一个函数,如下所示:

function class_replace_with(new_classname)
{
    var color1=html.className.split(' ')[0];
    html.className=html.className.substring(0, html.className.indexOf(' ')) + new_classname + html.className.substring(html.className.indexOf(' '));
}
因此,出于您的特定目的,将函数调用为:

class_replace_with('red');// as you have used red as the replacement for black.

这是否也会在类之间添加空格?这是否也会在类之间添加空格?这不是jqueryquestion@om_deshpande我知道,但我回答了,因为我想提供更多的选项,用不同的方法来做同样的事情。你通常应该只在没有标记jQuery的东西上发布纯js答案。这不是jQueryquestion@om_deshpande 我知道,但我回答了,因为我想提供更多选项,以不同的方式实现同样的效果。通常,你应该只在未标记jQuery的内容上发布纯js答案。你确定这是跨浏览器解决方案吗?@om_deshpande不是跨浏览器解决方案,除非你的受众是最新的(),但对于只修改元素上的单个类名的问题,这是最好的通用解决方案。不支持IE9吗?真的做不到。你确定这是一个跨浏览器的解决方案吗?@om_deshpande不是一个跨浏览器的解决方案,除非你的读者是最新的(),但它是解决在一个元素上只修改一个类名的问题的最好的通用解决方案。不支持IE 9吗?真的没办法。