Jquery g、 功能(m){ 返回m.toUpperCase(); }); }); a::before{ 内容:attr(title); }
这不能在CSS中完成,因为CSS只改变HTML的表示,而不改变底层数据或属性。因此,JavaScript是这种情况下的唯一解决方案。为此,我建议:Jquery g、 功能(m){ 返回m.toUpperCase(); }); }); a::before{ 内容:attr(title); },jquery,html,css,Jquery,Html,Css,这不能在CSS中完成,因为CSS只改变HTML的表示,而不改变底层数据或属性。因此,JavaScript是这种情况下的唯一解决方案。为此,我建议: // retrieving all elements with a title-attribute, using // document.querySelectorAll(), and converting that into an Array // using Array.from. // we then use Array.prototype.f
// retrieving all elements with a title-attribute, using
// document.querySelectorAll(), and converting that into an Array
// using Array.from.
// we then use Array.prototype.forEach() to iterate over each of
// those nodes:
Array.from(document.querySelectorAll('[title]')).forEach(function(el) {
// 'el' (the first argument) is the current Array-element of the
// Array over which we're iterating.
// here we use String.prototype.replace() to replace a the
// captured string `[a-z]` (lower-case only) which follows a
// a word-boundary (`\b`) finding all matches (g) from the supplied
// string, and using the anonymous function of the replace() method:
return el.title = el.title.replace(/\b([a-z])/g, function(m){
// here we return the match ('m') once we've converted it
// to uppercase using String.prototype.toUpperCase():
return m.toUpperCase();
});
});
Array.from(document.querySelectorAll('[title]')).forEach(函数(el){
返回el.title=el.title.replace(/\b([a-z])/g,函数(m){
返回m.toUpperCase();
});
});代码>
a::before{
内容:attr(title);
}
非常酷的更换解决方案!只是用querySelector
而不是queryselectoral
:)打错了,我在验证阶段就发现了。非常酷的替换解决方案!只是输入了querySelector
而不是queryselectoral
:)是的,我在验证阶段发现了这一点。
<a href="#" title="This Is Title Capitalize">
var elems = document.getElementsByTagName('a');
for (el in elems) {
if (el.hasAttribute('title') {
var str = el.getAttribute('title');
str = titleCase(str);
el.setAttribute('title', str);
}
}
function titleCase(str) {
str = str.toLowerCase().split(' ');
for(var i = 0; i < str.length; i++){
str[i] = str[i].split('');
str[i][0] = str[i][0].toUpperCase();
str[i] = str[i].join('');
}
return str.join(' ');
}
<script>
(function() {
[].forEach.call(document.querySelectorAll('[title]'), function(element) {
var words = element.title.split(/\s+/);
var capitalized = words.map(function(word) {
return word[ 0 ].toUpperCase() + word.slice(1);
});
element.title = capitalized.join(' ');
});
})();
</script>
// retrieving all elements with a title-attribute, using
// document.querySelectorAll(), and converting that into an Array
// using Array.from.
// we then use Array.prototype.forEach() to iterate over each of
// those nodes:
Array.from(document.querySelectorAll('[title]')).forEach(function(el) {
// 'el' (the first argument) is the current Array-element of the
// Array over which we're iterating.
// here we use String.prototype.replace() to replace a the
// captured string `[a-z]` (lower-case only) which follows a
// a word-boundary (`\b`) finding all matches (g) from the supplied
// string, and using the anonymous function of the replace() method:
return el.title = el.title.replace(/\b([a-z])/g, function(m){
// here we return the match ('m') once we've converted it
// to uppercase using String.prototype.toUpperCase():
return m.toUpperCase();
});
});