我在 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
不包含任何 thead
或 th
,所以这就是您的代码“不”工作的原因。
我已将 thead
和 th
添加到您的代码中,然后一切正常。
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/