jquery - 向字符串表添加样式不起作用

标签 jquery html css

我在 jquery 调用中有一个字符串 HTML,如下所示

var template = '<html >' +

'<head>' +
'<h1>Most important heading here</h1>'+

'</head>' +
'<body>' +

'<table border="2px" class="ExlTable"><tr bgcolor="#87AFC6">{table}</table>' +

'</body></html>';

我正在将现有表数据加载到此 {table}

我想设置这个表的大小th和td,我正在使用

$('th').css('width', '200');
$('th').css('height', '30');
$('td').css('height', '30');

这是可行的,但它会影响页面上的所有表格,所以我尝试了以下两种类型,但都不起作用

$('.ExlTable th').css('width', '200');
$('.ExlTable th').css('height', '30');
$('.ExlTable td').css('height', '30');

不工作

var $template = $(template);
$template.find('th').css('width', '200');
$template.find('th').css('height', '30');
$template.find('td').css('height', '30');

打不开,请帮忙

最佳答案

您的 template 不包含任何 theadth ,所以这就是您的代码“不”工作的原因。

我已将 theadth 添加到您的代码中,然后一切正常。

var template = '<html >' +

'<head>' +
'<h1>Most important heading here</h1>'+

'</head>' +
'<body>' +

'<table border="2px" class="ExlTable"><thead><tr><th>TableHeader</th></tr></thead><tbody><tr bgcolor="#87AFC6"><td>{table}</td></tbody></table>' +

'</body></html>';

var $template = $(template);
$template.find('th').css('width', '200');
$template.find('th').css('height', '30');
$template.find('td').css('height', '30');

$(".tableContainer").html($template)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="tableContainer"></div>

关于jquery - 向字符串表添加样式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56846169/

相关文章:

在div中压缩的固定大小的html表格单元格

CSS。 3 列布局,带有固定位置的侧列

javascript - mouseenter + mouseleave/hover 在两个不同的 div 上

html - 如何在溢出 :hidden 的容器中显示纯 css 工具提示

c# - 在 jquery 模态对话框中定义 ui-widget-content 的边框

jquery - Mustache 和 Jquery 插件问题

javascript - 为什么这不是一个有效的链式函数?

JQuery 更改所选列表项的背景颜色

jquery - 滚动条显示何时调用 jquery.show()

javascript - jQuery:ajaxSubmit/ajaxForm 有什么显着区别吗?