代码之家  ›  专栏  ›  技术社区  ›  Justin Alexander

是否可以在HTML class属性中传递参数?

  •  1
  • Justin Alexander  · 技术社区  · 14 年前

    我希望能够将某些信息嵌入到HTML元素(图像)中,而不破坏有效的HTML标记。以下是有效的HTML吗?

    <img src="..." class="isearcher:tags(paris+hilton,gary+suneese)" >
    

    希望在javascript/jQuery中使用regex来提取它。

    1 回复  |  直到 14 年前
        1
  •  3
  •   Nick Craver    14 年前

    格式略有不同,但有一个专门的插件, the metadata plugin

    <img src="..." class="isearcher {tags : ['paris hilton', 'gary suneese']}" >
    

    下面是一个使用插件获取数据的示例:

    var data = $("img").metadata();
    data.tags        // this is an array with both tags
    data.tags.length // 2
    data.tags[0]     // "paris hilton"
    

    You can try it out here

    如果你使用的是HTML5, take a look at data attributes