有沒有一種方法可以對標籤中的部分文本進行樣式設置 - 更改顏色,粗體,大小等?GWT/CSS - 樣式標籤的一部分
2
A
回答
3
使用HTML小部件而不是Label。然後:
HTML label = new HTML();
label.setHtml("Brown <span class=\"brown\">fox</span>");
-1
我有點無聊,我想我也許能提供一些有用的東西,所以,這麼說,我提供這個:
function elemStyle(el, needle, settings) {
// if there's no 'el' or 'needle' arguments, we quit here
if (!el || !needle) {
return false;
}
else {
// if 'el' has a nodeType of 1, then it's an element node, and we can use that,
// otherwise we assume it's the id of an element, and search for that
el = el.nodeType == 1 ? el : document.getElementById(el);
// if we have a 'settings' argument and it's an object we use that,
// otherwise we create, and use, an empty object
settings = settings && typeof settings === 'object' ? settings : {};
// defining the defaults
var defaults = {
'class': 'presentation',
'elementType': 'span'
},
// get the text from the 'el':
haystack = el.textContent || el.innerText;
// iterate over the (non-prototypal) properties of the defaults
for (var prop in defaults) {
if (defaults.hasOwnProperty(prop)) {
// if the 'settings' object has that property set
// we use that, otherwise we assign the default value:
settings[prop] = settings[prop] || defaults[prop];
}
}
// defining the opening, and closing, tags (since we're using HTML
// as a string:
var open = '<' + settings.elementType + ' class="' + settings.class + '">',
close = '</' + settings.elementType + '>';
// if 'needle' is an array (which is also an object in JavaScript)
// *and* it has a length of 2 (a start, and stop, point):
if (typeof needle === 'object' && needle.length === 2) {
var start = needle[0],
stop = needle[1];
el.innerHTML = haystack.substring(0, start) + open + haystack.substring(start, stop) + close + haystack.substring(stop);
}
// otherwise if it's a string we use regular expressions:
else if (typeof needle === 'string') {
var reg = new RegExp('(' + needle + ')');
el.innerHTML = haystack.replace(reg, open + '$1' + close);
}
}
}
上述被稱爲象所以:
// a node-reference, and a string:
elemStyle(document.getElementsByTagName('label')[0], 'Input');
// a node-reference, and a start-stop array:
elemStyle(document.getElementsByTagName('label')[0], [6, 8]);
// an id (as a string), and a string to find, with settings:
elemStyle('label1', 'Input', {
'elementType' : 'em'
});
這肯定可以處理一些錯誤(例如,如果一個數組被傳入的函數少於或多於兩個元素nothing happens,並且沒有錯誤返回給用戶/開發人員;如果el
變量既不是節點引用也不是id
,things go wrong: Uncaught TypeError: Cannot read property 'textContent' of null
)。
說了這麼多,我覺得髒,所以我在一個簡單的錯誤檢查,並將報告,如果el
不能解決到文檔中的實際節點:
el = el.nodeType == 1 ? el : document.getElementById(el);
// if 'el' is null, and therefore has no value we report the error to the console
// and then quit
if (el === null) {
console.log("You need to pass in either an 'id' or a node-reference, using 'document.getElementById(\"elemID\")' or 'document.getElementsByTagName(\"elemTag\")[0].");
return false;
}
參考文獻:
+0
這與GWT的Label和HTML小部件完全沒有關係! @Andre Volgin答案簡單而正確!!!!! – SSR
+0
@SSR:不,但它*符合JavaScript的要求;現在:如果與標籤相關的答案是*不正確*該標籤不應該在那裏。 –
相關問題
- 1. 如何覆蓋頁面上某一部分的標籤樣式?
- 2. Android 4.0的副標題(部分)標籤樣式
- 3. HTML註釋標籤內部樣式表
- 4. 標籤混亂中的樣式標籤
- 5. 部分形式的div標籤問題
- 6. 樣式ActionBar標籤
- 7. WPF標籤樣式
- 8. TabLayout標籤樣式
- 9. 外部樣式表與JavaScript添加樣式標籤
- 10. Css標題標籤樣式
- 11. keygen標籤的CSS樣式
- 12. MVC樣式的標籤
- 13. 標籤的HTML樣式
- 14. 使用一個標籤CSS樣式的其他標籤?
- 15. 標籤樣式代替類樣式
- 16. 鼠標滾輪使用部分標籤滾動到部分。怎麼樣?
- 17. Doxygen FAQ部分的標籤
- 18. 用一樣的Meta標籤
- 19. CSS - 樣式圖像標籤的標題
- 20. bbcode樣式標籤與preg
- 21. 獨立樣式Bootstrap標籤
- 22. javascript選擇樣式標籤?
- 23. IE7 THEAD標籤樣式
- 24. 未知「樣式」標籤
- 25. 選擇標籤樣式
- 26. 在使用樣式標籤
- 27. 添加樣式標籤頁
- 28. pygtk全局標籤樣式
- 29. CSS標籤樣式Vaadin
- 30. 在列表項標籤內部的錨標籤上添加樣式
是的,但解釋標籤的'部分'是什麼意思。一個特定的詞或屬性(顏色,字體重量等)..? –
一些文字 - 只有第三個作品,例如 –
只有第三個詞? –