从父选择器获取属性是CSS使用内容:attr(); [英] getting attribute from parent selector is CSS using content:attr();

查看:94
本文介绍了从父选择器获取属性是CSS使用内容:attr();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将<节>的标题属性< h3>标签内的标签位于< section>下方和内部的标签标签。以下作品

 < section title =一般信息> 
< h3>一般资讯< / h3>
....
< / section>

我不想重复< h3>标签,但使用CSS内容和attr()来获取< section>中的tittle属性内的信息。标记并将其放在< h3>

 < section title =一般信息> 
< h3>< / h3>
....
< / section>

部分h3:在
之后{
content:attr(title);
}

如预期的那样在< h3>内寻找'title'属性;标记而不是< section>标签。有什么办法可以实现我想要完成的目标吗?我正在努力完成正确的做事方式吗?我知道我可以跳过< h3>标记完成并格式化<节>标签以与< h3>相同的方式显示标题属性中的任何文本。标签会。

解决方案

属性来自生成内容的元素。您将无法获取部分元素的 title ,因为它不是生成内容的元素。 p>

正如ScottS所提到的那样,您应该避免使用空标题元素,因为根本没有必要拥有它。你最好的办法是省去 title 属性(真的需要吗?),或者保留内容重复。


Hey I am trying to place the 'title' attribute of a <section> tag inside a <h3> tag located just below and inside the <section> tag.The following works

   <section title="General Information">
       <h3>General Information</h3>
       ....
   </section>

what I would like to do is not repeat 'General Information' inside the <h3> tag but use CSS content along with attr() to get the info inside of the tittle attribute in the <section> tag and place it inside the <h3> tags.

   <section title="General Information">
       <h3></h3>
       ....
   </section>

   section h3:after 
   {
       content:attr(title);
   }

This as expected looks for the 'title' attribute inside of the <h3> tag instead of the <section> tag. Is there any way I can achieve what I am trying to accomplish? Is what I am trying to accomplish the right way of doing things? I know I could skip the <h3> tags altogether and format the <section> tag to display whatever text is in 'title' attribute the same way that the <h3> tag would.

解决方案

The attr() function can only obtain an attribute from the element that's generating the content. You won't be able to get the section element's title because it's not the element that's generating the content.

As ScottS mentions, you should really avoid having an empty heading element, as there wouldn't be a point in having it there at all then. Your best bet is to either leave out the title attribute (is it really needed?), or leave the content duplicated.

这篇关于从父选择器获取属性是CSS使用内容:attr();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆