如何设计元素的最后一个单词的样式?
给定一个动态加载的标题(使用 PHP/WordPress),是否有一种纯 CSS 方法来设置最终单词的样式?例如:
<h1 class='featured'> This is my page title</h1>
如果使用 CSS不可行
<h1 class='featured'> This is my page <span id="finalWord">title</span></h1>
,那么分解
标记的内容是否是建议的方法?
Given a dynamically loaded heading (using PHP/WordPress) is there a pure CSS method of styling the final word? For instance:
<h1 class='featured'> This is my page title</h1>
Becomes
<h1 class='featured'> This is my page <span id="finalWord">title</span></h1>
If using CSS is not viable, is exploding the content of the <h1>
tag the suggested way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AFAIK CSS 中没有
:last-word
伪类(尽管这很酷)。您可以使用 JavaScript 来完成类似的事情,但如果您有权访问服务器端,我会使用 PHP 来完成。它将成为源代码的一部分,并且可能更容易。
一个简单的算法是使用 strrpos() 并使用 substr() 将其重新组合在一起,包裹在 <代码><跨度>。
AFAIK there is not a
:last-word
pseudo class in CSS (although that would be cool). You could use JavaScript for something like this, but if you have access to the<h1>
server-side I'd do it with PHP. It's going to be part of the source code and probably easier.A simple algorithm would be to find the last space in the string with strrpos() and use substr() to piece it back together wrapped in
<span>
.我想一个非常粗略的方法是创建一种处理所有单词的方法并将其放入 PHP
array
中。然后echo
所有值,如果是最后一个值,则将放在前面,将
放在前面。
之后。例如:
第 1 步:创建数组
现在您已将所有数据存储在数组中。每个词都在它的对象中。
所以基本上这段代码的作用是计算数组中有多少个对象,并检查显示的对象是否是最后一个。如果是,它就会在上面贴上正确的 ID。
我刚刚快速输入了这段代码,所以可能会有一些错误。
库尔顿
I guess a really crude way would be to create a way to all the words and put it in a PHP
array
. Thenecho
all the values, and if it's the last one then put the<span id="finalWord">
before and the</span>
after.EX:
Step 1: Create the array
Now you have all your data in a array. Each word is in it's object.
So basically what this code does is count how many objects are in your array and will check if the object being displayed is the last one. If it is, it will put the correct ID on it.
I just typed this code out real quick, so there could be some errors.
Coulton
原则上没有这样的方法。 CSS 无法修改 DOM。
获取 dom 元素的文本,使用您自己的“单词”标准找到最后一个单词,将其包装到 span 中并根据结果设置innerHTML。
No such method in principle. CSS cannot modify the DOM.
Take text of dom element, find last word using your own criteria for the "word", wrap it into span and set innerHTML by the result.
抱歉,对一篇古老的帖子进行了跟进,这是我在谷歌搜索“使用 php 用跨度标签包装字符串中的最后一个单词”时出现的帖子,所以我想这就是我放置解决方案的地方跟上。
以上述为起点,我创建了这个函数来完成我所需要的:
Sorry for the follow up on an ancient post, this is the one that came up in my google searches for "wrap the last word in a string with a span tag using php" so I figured this is where I would put the solution I came up with.
Using the above as a starting point, I created this function to accomplish what I needed: