javascript 将表格或 td 标签的内容设为粗体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28329833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make contents of a table or td tag bold
提问by Tai
I'm working with some legacy code and encountered the following issue.
我正在处理一些遗留代码并遇到以下问题。
I'm using YAHOO.widget.DataTable( elContainer, aColumnDefs, oDataSource, oConfigs ) structure to modify html elements in a table.
我正在使用 YAHOO.widget.DataTable( elContainer, aColumnDefs, oDataSource, oConfigs ) 结构来修改表格中的 html 元素。
I am running a basic number to US currency conversion on the code in one td of this table. It works for other tables however it fails in this table because all elements are set to bold via the "< \b >" tag which is being pulled into the Yahoo Table structure as the td value.
我正在此表的一个 td 中的代码上运行一个基本数字到美国货币的转换。它适用于其他表格,但在此表格中失败,因为所有元素都通过“<\b>”标记设置为粗体,该标记作为 td 值被拉入雅虎表格结构。
Ex:
前任:
<td>
<b><s:property value="totalTransactions"/> my text </b>
</td>
For example when the table queries the value at this td it gets
"<b><s:property value="totalTransactions"/> my text</b>".
When really all I want is my text.
例如,当表查询此 td 处的值时,它会得到
"<b><s:property value="totalTransactions"/> my text</b>".
当我真正想要的是我的文本时。
My preffered solution is to remove the b tags.
我首选的解决方案是删除 b 标签。
So the code can run (and doesn't need to differ between different tables and it doesn't need to clean them all first and then attempt to distinguish when it should add bold tags back)
所以代码可以运行(并且不需要在不同的表之间有所不同,也不需要先清理它们然后尝试区分何时应该添加粗体标签)
For example I could check if html exists remove it, modify the number, and then add the html back. I think it's cleaner (personal preference perhaps) to modify the table so that it keeps the bold font for all elements in it without adding text inside the td tag.
例如,我可以检查 html 是否存在删除它,修改数字,然后将 html 添加回来。我认为修改表格更简洁(可能是个人偏好),以便它保留其中所有元素的粗体字体,而无需在 td 标签内添加文本。
I looked at: Bold a single line in html table <td>
我看了看:Bold a single line in html table <td>
I tried
我试过
<td style="font-weight:bold"> my text </td>
However it does not become bold.
然而,它并没有变得大胆。
the bold tag makes the text bold when done like this:
像这样完成时,粗体标记使文本变粗:
<td>
<b>my text</b>
</td>
However the b tag interferes with my function. My thought that is due to the yahoo injection:
但是 b 标签干扰了我的功能。我的想法是由于雅虎注入:
result = "$"+myAmount;
el.innerHTML = result;
That the bold style might be overwritten. Are there any solutions? Or is there another reason style doesn't work?
粗体样式可能会被覆盖。有什么解决办法吗?还是风格不起作用的另一个原因?
I believe the issue is due to the fact that I'm using a struct: see http://www.mkyong.com/struts2/struts-2-property-tag-example/.
我相信这个问题是由于我使用了结构:参见http://www.mkyong.com/struts2/struts-2-property-tag-example/。
I am unsure of why traditional styling rules do not apply. Or perhaps I have made an error? The page renders fine just without any bold. I see no logged errors.
我不确定为什么传统的样式规则不适用。或者也许我犯了一个错误?页面呈现良好,没有任何粗体。我没有看到记录的错误。
回答by Емил Цоков
From what I understood form your question there are 2 possible solutions with inline css.
根据我对您的问题的理解,内联 css 有两种可能的解决方案。
First one
第一
<html>
<head>
<style>
table td {
font-weight:bold;
}
</style>
</head>
<body>
<table><tr><td>Text inside table cell</td></tr></table>
</body>
</html>
Second one
第二个
<table>
<tr><td style="font-weight:bold">Text inside the table</td></tr>
</table>
one with css declaration:
一个带有 css 声明的:
table td {
font-weight:bold;
}
Another solution will be using strong tag to wrap the text.
另一种解决方案是使用强标签来包装文本。
<table><tr><td><strong>Text goes here</strong></td></tr></table>
回答by Magu
I hope I understood the question correctly and your goal is to bold all text in one table. This inline style makes all texts in the cells from the table bold:
我希望我正确理解了这个问题,你的目标是在一张表格中加粗所有文本。这种内联样式使表格中单元格中的所有文本都变为粗体:
<table style="font-weight: 700;">...
or a more readable version:
或更易读的版本:
<table style="font-weight: bold;">...
回答by Angry 84
While the other two answer at the moment have covered the basics, using either strong tag or using a css style (inline or in a style dec).
虽然目前其他两个答案已经涵盖了基础知识,使用强标签或使用 css 样式(内联或样式 dec)。
If the above two still fail, perhaps another means would be using javascript or jquery to enumerate over the table and apply a bold style afterwards either directly to each td cell or table by applying a css or inserting your own b/strong tags.
如果以上两个仍然失败,也许另一种方法是使用 javascript 或 jquery 枚举表格,然后通过应用 css 或插入您自己的 b/strong 标签直接对每个 td 单元格或表格应用粗体样式。
This may not be an option in your case, but being others will see this. There are still times when this is called for.
在您的情况下,这可能不是一种选择,但其他人会看到这一点。有时仍然需要这样做。
I wont post any code as the basics are covered, unless you need a Javascript/jQuery code.
我不会发布任何代码,因为涵盖了基础知识,除非您需要 Javascript/jQuery 代码。