gpt4 book ai didi

javascript - 无法链接 CSS 和 Javascript

转载 作者:行者123 更新时间:2023-11-28 01:41:54 25 4
gpt4 key购买 nike

我正在尝试用 HTML 创建可调整大小的表格。因为,那里的数据将具有不同的长度。但无法将 css 和 js 代码与 HTML 链接起来。它们分别位于同一目录中创建的单独文件夹中。我也尝试将它们保存在同一个文件夹中,但没有加载。Chrome 开发者模式也没有显示任何错误,但仍然只是如果有任何其他有效的方法来调整表的大小,请提出建议。下面的js代码。

$(function(){
var thHeight = $("table#demo-table th:first").height();
$("table#demo-table th").resizable({
handles: "e",
minHeight: thHeight,
maxHeight: thHeight,
minWidth: 40,
resize: function (event, ui) {
var sizerID = "#" + $(event.target).attr("id") + "-sizer";
$(sizerID).width(ui.size.width);
}
});
});

下面是CSS

body {
font-family: Arial;
font-size: 10pt;
}
table#demo-table th {
background-color: #006;
color: #fff;
}
table#demo-table th,
table#demo-table td {
white-space: nowrap;
padding: 3px 6px;
}
table.cellpadding-0 td {
padding: 0;
}
table.cellspacing-0 {
border-spacing: 0;
border-collapse: collapse;
}
table.bordered th,
table.bordered td {
border: 1px solid #ccc;
border-right: none;
text-align: center;
}
table.bordered th:last,
table.bordered td:last {
border-right: 1px solid #ccc;
}

下面是HTML

<!DOCTYPE html>
<html>
<head>
<link ref="stylesheet" type="text/css" href="css/democss.css">
<script type="text/JavaScript" src="js/demojs.js" ></script>
</head>
<body>
<table id="demo-table" class="bordered cellpadding-0 cellspacing-0">
<thead>
<tr>
<th id='column-header-1'>Column Header 1<div id='column-header-1-sizer'></div></th>
<th id='column-header-2'>Column Header 2<div id='column-header-2-sizer'></div></th>
<th id='column-header-3'>Column Header 3<div id='column-header-3-sizer'></div></th>
<th id='column-header-4'>Column Header 4<div id='column-header-4-sizer'></div></th>
</tr>
</thead>
<tbody>
<td>My Data 1</td>
<td>My Data 2</td>
<td>My Data 3</td>
<td>My Data 4</td>
</tbody>
</table>

</body>

最佳答案

您缺少用于 .resizable() 方法的 jQuery 库和 jQuery UI,以及 jQuery UI CSS 支持

<link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />

<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/JavaScript" src="js/demojs.js" ></script>

例子:

<!DOCTYPE html>
<html>
<head>
<title>Resizable TH</title>
<link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<style>
/* instead of style use <link> this is a demo.*/
body {
font-family: Arial;
font-size: 10pt;
}

table#demo-table th {
background-color: #006;
color: #fff;
}

table#demo-table th,
table#demo-table td {
white-space: nowrap;
padding: 3px 6px;
}

table.cellpadding-0 td {
padding: 0;
}

table.cellspacing-0 {
border-spacing: 0;
border-collapse: collapse;
}

table.bordered th,
table.bordered td {
border: 1px solid #ccc;
border-right: none;
text-align: center;
}

table.bordered th:last,
table.bordered td:last {
border-right: 1px solid #ccc;
}
</style>
</head>
<body>
<table id="demo-table" class="bordered cellpadding-0 cellspacing-0">
<thead>
<tr>
<th id='column-header-1'>Column Header 1<div id='column-header-1-sizer'></div></th>
<th id='column-header-2'>Column Header 2<div id='column-header-2-sizer'></div></th>
<th id='column-header-3'>Column Header 3<div id='column-header-3-sizer'></div></th>
<th id='column-header-4'>Column Header 4<div id='column-header-4-sizer'></div></th>
</tr>
</thead>
<tbody>
<td>My Data 1</td>
<td>My Data 2</td>
<td>My Data 3</td>
<td>My Data 4</td>
</tbody>
</table>

<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
// Use <script> with src to your .js file. This is a demo.
jQuery(function($) {
var thHeight = $("table#demo-table th:first").height();
$("table#demo-table th").resizable({
handles: "e",
minHeight: thHeight,
maxHeight: thHeight,
minWidth: 40,
resize: function(event, ui) {
var sizerID = "#" + $(event.target).attr("id") + "-sizer";
$(sizerID).width(ui.size.width);
}
});
});
</script>

</body>
</html>

关于javascript - 无法链接 CSS 和 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50397206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com