简体   繁体   English

是否可以按类名定义标题属性?

[英]Is it possible to define a title attribute by class name?

I would like to make all .note elements have a specific tooltip. 我想让所有.note元素都有一个特定的工具提示。

Is it possible to do this automatically, eg in CSS? 是否可以自动执行此操作,例如在CSS中? Just like the note class has colors and a font defined, is it possible to bind the tooltip text (the title attribute) with that class? 就像note类有颜色和定义的字体一样,是否可以将工具提示文本( title属性)与该类绑定?

I could make some function that iterates over all .note elements and append the title attribute if it's not present yet. 我可以创建一些迭代所有.note元素的函数,如果它还不存在则追加title属性。 However, since I don't have to iterate manually for CSS styles, I was wondering if there is a more elegant solution to this problem as well (eg is it possible to define it in CSS?). 但是,由于我不必手动迭代CSS样式,我想知道是否有更优雅的解决方案来解决这个问题(例如,是否可以在CSS中定义它?)。

You can't change the attributes of an element using CSS; 您无法使用CSS更改元素的属性; you could, perhaps, emulate the title functionality, though (albeit only clumsily): 或许,您可以模拟title功能(虽然只是笨拙):

.note {
    position: relative;
}

.note:after {
    content: "The alternate 'pseudo-title' text for the .note elements.";
    position: absolute;
    left: 50%;
    background-color: #000;
    color: #fff;
    }

JS Fiddle demo . JS小提琴演示

However using this will still allow the original title to show, so I'd suggest removing that attribute with JavaScript (or in a server side language). 但是,使用它仍然会允许显示原始title ,因此我建议使用JavaScript(或服务器端语言)删除该属性。

Possible JavaScript title-removal method: 可能的JavaScript标题删除方法:

var notes = document.getElementsByClassName('note');
var notesNum = notes.length;

for (i=0; i<notesNum; i++){
    notes[i].removeAttribute('title');
}

JS Fiddle demo (combining the two approaches) . JS Fiddle演示(结合两种方法)


Edited , as the OP, from comments (below) doesn't want to change the existing look and feel, to add a JavaScript option to assigning a new title for all elements with the class-name of note : 编辑 ,作为OP,从评论(下面)不想改变现有的外观,添加一个JavaScript选项为所有元素分配一个新的标题,其中类名为note

var notes = document.getElementsByClassName('note');
var notesNum = notes.length;
var newTextForNotesClass = "This is the new title text for the notes-class-name group.";

for (i=0; i<notesNum; i++){
    notes[i].title = newTextForNotesClass;
}

JS Fiddle demo . JS小提琴演示

That's not something that's within the scope of CSS. 这不是CSS范围内的东西。 Your best bet is to use some JavaScript or JQuery. 最好的办法是使用一些JavaScript或JQuery。

As you asked the question with 'eg CSS', I'm presuming you want an answer as to how to use a class to assign a title. 当你用'eg CSS'问这个问题时,我假设你想要一个关于如何使用类来分配标题的答案。 Like the other answers, jQuery or javascript is your best bet. 像其他答案一样,jQuery或javascript是你最好的选择。

With jQuery, you can do: 使用jQuery,您可以:

tooltip = "Tooltip Text";
$('.note').attr('title', tooltip);

Here is a fiddle with an example: http://jsfiddle.net/RdNvy/ 这是一个例子的小提琴: http//jsfiddle.net/RdNvy/

CSS is for styling not for carrying out action; CSS用于不用于执行操作的样式; you would need JavaScript for that sort of thing. 你需要JavaScript才能做到这一点。 You could use document.getElementsByClassName to get an array that you could for loop through setting those title attributes. 您可以使用document.getElementsByClassName来获取可以循环设置这些标题属性的数组。

I'm not sure, but try to loot at css "content" property. 我不确定,但试着掠夺css“content”属性。 http://www.w3schools.com/cssref/pr_gen_content.asp http://www.w3schools.com/cssref/pr_gen_content.asp

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

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