简体   繁体   English

HTML中的多个类属性

[英]Multiple class attributes in HTML

What happens when an element has multiple class attributes? 当元素具有多个class属性时会发生什么?

<div id="test" class="one two three" class="four">

I'm trying to add a class to the output of post_class(); 我正在尝试将一个类添加到post_class();的输出中post_class(); in a WordPress plugin, but the function itself is creating the entire part class="one two three" 在WordPress插件中,但函数本身正在创建整个部分class="one two three"

Is it equivalent to class="one two three four" ? 它等同于class="one two three four"吗? Or does the first or second win? 或者第一次或第二次获胜? Or is it undefined behaviour, in which case what do the major browsers do? 或者它是未定义的行为,在这种情况下主要的浏览器做什么?

If you know the correct way of adding a class to this snippet (WordPress plugin), then that would also be appreciated! 如果您知道向此片段(WordPress插件)添加类的正确方法,那么也将不胜感激!

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

What happens when an element has multiple class attributes? 当元素具有多个类属性时会发生什么?

When an attribute is declared multiple times for a single element (which is invalid HTML, by the way), behavior-wise the first value will override all subsequent values for the same attribute. 对于单个元素(顺便说一下,这是无效的HTML)多次声明属性时,行为方面第一个值将覆盖同一属性的所有后续值。 So in this case, your element will only have the classes one two three . 所以在这种情况下,你的元素只有one two three类。

This behavior is explained in the HTML5 spec, 8.2.4.35 Attribute name state , "... if there is already an attribute on the [element] with the exact same name, then this is a parse error and the new attribute must be removed..." HTML5规范8.2.4.35属性名称状态中解释了这种行为,“...如果[元素]上已经有一个具有完全相同名称的属性,那么这是一个解析错误,必须删除新属性......”

If you know the correct way of adding a class to this snippet (WordPress plugin), then that would also be appreciated! 如果您知道向此片段(WordPress插件)添加类的正确方法,那么也将不胜感激!

Typically, if you need to add custom classes dynamically to your WordPress posts, you hook onto the post_class filter and manipulate the $classes array as necessary. 通常,如果您需要动态地向WordPress帖子添加自定义类,您可以挂钩到post_class过滤器并根据需要操作$classes数组。 Here's what it roughly looks like in my themes: 这是我在主题中大致看起来的样子:

function nv_post_class( $classes ) {
    // Most recent post on the front page
    global $count;
    if ( is_home() && 1 == $count )
        $classes[] = 'latest-post';

    return $classes;
}

add_filter( 'post_class', 'nv_post_class' );

If you only need to add one or more static classes, pass them as a space-delimited string directly to post_class() : 如果只需要添加一个或多个静态类,请将它们作为空格分隔的字符串直接传递给post_class()

<div id="post-<?php the_ID(); ?>" <?php post_class( 'myclass1 myclass2' ); ?>>

More on this in the WordPress Codex . 更多关于WordPress Codex的内容

Then the document will be invalid and the browser will attempt to perform error recovery. 然后文档将无效,浏览器将尝试执行错误恢复。

From the HTML 5 specification : HTML 5规范

If the attribute's name is already in attribute list, then return to the step labeled attributes. 如果属性的名称已在属性列表中,则返回标记为属性的步骤。

So, if an HTML 5 parser is being used, the first attribute should apply. 因此,如果正在使用HTML 5解析器,则应该应用第一个属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM