admin管理员组文章数量:1397031
I need to show user's list which should look like in the example below:
Helen Burns
Edward Fairfax Rochester
Bertha Antoinetta Mason
Adèle Varens
Is there a way to achieve this without using javascript? Each row should be one span, i.e. <span>Helen</span><span>Burns</span>
is not acceptable.
I need to show user's list which should look like in the example below:
Helen Burns
Edward Fairfax Rochester
Bertha Antoinetta Mason
Adèle Varens
Is there a way to achieve this without using javascript? Each row should be one span, i.e. <span>Helen</span><span>Burns</span>
is not acceptable.
- What about JS that changing the HTML structure (wrapping last words with <span>) at runtime? – Makram Saleh Commented Jul 20, 2010 at 13:26
- Would <span>Helen <span>Burns</span></span> be acceptable? Otherwise there's no :last-word CSS selector. – jasongetsdown Commented Jul 20, 2010 at 13:28
- 1 You need to be more specific, I think. This could easily be done by doing <span>Helen <strong>Burns</strong></span>, but something tells me you may be trying to do something else. – davidcelis Commented Jul 20, 2010 at 13:29
- What if someone has two last names? you can't select only the last word, there has to be some sort of semantics. – jao Commented Jul 21, 2010 at 5:48
6 Answers
Reset to default 8No, there is not. You are going to have to use some form of scripting to acplish this if you don't want your last names to be in their own tags.
To the browser, each row is an element, and the "words" themselves have no separate meaning as far as CSS is concerned. You must place the words in different tags in order to do what you want.
The browser does not automagically know what part of the name is the last name so you have to add extra markup to achieve what you want.
There's no solution for mon used browser for know using only CSS. You should use javascript or HTML + CSS as you already made.
without pure css this is impossible (as you don't want a separation in the markup)...
<span>Monty Burns</span><br />
<span>Bart Simpson</span>
<script type="text/javascript">
$(document).ready(function() {
var spans = $('span');
spans.each(function(index, element) {
var span = $(element);
var spanText = span.text();
var spanTextArray = spanText.split(' ');
var spanTextArrayLength = spanTextArray.length;
var lastName = spanTextArray[spanTextArrayLength -1];
spanTextArray.pop();
var firstName = spanTextArray.join(' ');
span.text(firstName);
var spanLastName = $('<span/>');
spanLastName.css('font-weight', 'bold');
spanLastName.css('margin-left', '5px');
spanLastName.appendTo(span);
spanLastName.text(lastName);
});
});
</script>
working demo.
edit: if you do not want an extra span
-tag in there, just change
var spanLastName = $('<span/>');
spanLastName.css('font-weight', 'bold');
to
var spanLastName = $('<strong/>');
I don't think this is possible with CSS because your example doesn't show any order:
Helen Burns
Edward Fairfax Rochester
Bertha Antoinetta Mason
Adèle Varens
I don't know why you might desire not to have an extra tag surrounding the last name (as other answers and ments have suggested), but if you are looking simply for minimalist mark-up, this works (no span
even used):
Html:
<body>
First Middle <strong>Last</strong>
First Middle <strong>Last</strong>
First Middle <strong>Last</strong>
</body>
Css:
strong:after {content:' '; display: block;}
Which creates "rows" with your desired styling without anything more than a single tag (which could be a span
rather than a strong
if you desired).
Edit: Of course, this will not work for IE7 or under.
本文标签: javascriptIs there a crossbrowser css way to decorate only last word in a span blockStack Overflow
版权声明:本文标题:javascript - Is there a cross-browser css way to decorate only last word in a span block? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744148548a2592954.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论