在javascript中创建公共类
我有一个html页面,其中包含一些div和div内的表。使用在javascript中创建公共类,javascript,html,css,class,oop,Javascript,Html,Css,Class,Oop,我有一个html页面,其中包含一些div和div内的表。使用XHTMLREQUEST获取数据。我怎样才能把它转换成包装类,这样我就可以在项目中随时使用它 我可以创建id为的div,并将该特定的rturl用于加载数据。我怎样才能使它变得普通,这样我就可以在任何地方使用它 我的HTML代码: <html> <head> <style> * { box-sizing: border-box; } #myInput { background-image: ur
XHTMLREQUEST
获取数据。我怎样才能把它转换成包装类,这样我就可以在项目中随时使用它
我可以创建id为的div,并将该特定的rturl用于加载数据。我怎样才能使它变得普通,这样我就可以在任何地方使用它
我的HTML代码:
<html>
<head>
<style>
* {
box-sizing: border-box;
}
#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
width: 30%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}
#myTable {
border-collapse: collapse;
width: 30%;
height : 30%;
overflow-y:auto;
border: 1px solid #ddd;
font-size: 18px;
display: none;
margin-top:-12px;
}
#myTable th, #myTable td {
text-align: left;
padding: 12px;
width : 10%;
}
#myTable tr {
border-bottom: 1px solid #ddd;
order-top: 1px solid #ddd;
}
#myTable tr.header, #myTable tr:hover {
background-color: #f1f1f1;
}
#myInput:focus + #myTable{
display: block;
}
#myTable:hover{
display: block;
}
</style>
</head>
<body>
<h2>My Customers</h2>
<input type="text" id="myInput" onkeyup="myFunction()" title="Type in a name">
<table id="myTable">
<tr class="header"></tr>
<tr><td>Germany</td></tr>
<tr><td>Sweden</td></tr>
<tr><td>UK</td></tr>
<tr><td>Germany</td></tr>
<tr><td>Canada</td></tr>
<tr><td>Italy</td></tr>
<tr><td>UK</td></tr>
<tr><td>France</td></tr>
</table>
<script>
function myFunction() {
var input, filter, table, tr, td, i;
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
document.querySelectorAll('#myTable tr:not(.header)').forEach(function(_tr){
_tr.addEventListener('click',function(){
document.getElementById('myInput').value += " "+ this.getElementsByTagName('td')[0].textContent;
});
});
</script>
</body>
</html>
* {
框大小:边框框;
}
#我的输入{
背景图片:url('/css/searchicon.png');
背景位置:10px 10px;
背景重复:无重复;
宽度:30%;
字体大小:16px;
填充:12px 20px 12px 40px;
边框:1px实心#ddd;
边缘底部:12px;
}
#我的桌子{
边界塌陷:塌陷;
宽度:30%;
身高:30%;
溢出y:自动;
边框:1px实心#ddd;
字号:18px;
显示:无;
边缘顶部:-12px;
}
#我的表th,#我的表td{
文本对齐:左对齐;
填充:12px;
宽度:10%;
}
#myTable tr{
边框底部:1px实心#ddd;
订单顶部:1px实心#ddd;
}
#myTable tr.header,#myTable tr:悬停{
背景色:#f1f1;
}
#myInput:focus+#myTable{
显示:块;
}
#myTable:悬停{
显示:块;
}
我的顾客
德国
瑞典
英国
德国
加拿大
意大利
英国
法国
函数myFunction(){
var输入、过滤器、表格、tr、td、i;
table=document.getElementById(“myTable”);
tr=table.getElementsByTagName(“tr”);
对于(i=0;i-1){
tr[i].style.display=“”;
}否则{
tr[i].style.display=“无”;
}
}
}
}
document.querySelectorAll('#myTable tr:not(.header)').forEach(函数(_tr){
_tr.addEventListener('click',function(){
document.getElementById('myInput')。值+=“”+this.getElementsByTagName('td')[0]。textContent;
});
});
这就是我加载数据的方式
function foo(callback) {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', "data.json",true);
httpRequest.onreadystatechange = function () {
if(httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status === 200) {
// trigger your callback function
callback(httpRequest.responseText);
}
};
httpRequest.send();
}
foo(function(data) {
var jsonc = JSON.parse(data);
var new_opt="";
for(i=0;i<jsonc.length;i++)
{
new_opt+='<option value="'+jsonc[i]['VALUE']+'">'+jsonc[i]['VALUE']+'</option>';
}
document.getElementById('choose').innerHTML =new_opt;
});
函数foo(回调){
var httpRequest=new XMLHttpRequest();
open('GET','data.json',true);
httpRequest.onreadystatechange=函数(){
if(httpRequest.readyState==XMLHttpRequest.DONE&&httpRequest.status==200){
//触发你的回调函数
回调(httpRequest.responseText);
}
};
httpRequest.send();
}
foo(函数(数据){
var jsonc=JSON.parse(数据);
var new_opt=“”;
对于(i=0;i您不能使用类而不是ID吗?我想让它成为通用的。一个平面JS代码。在其中,我可以创建html和其他内容。您可能需要一个选择器(数据属性、类等),或者您可以添加一个onclick=“foo”你不能用类来代替ID吗?我想让它成为通用的。一个平面JS代码。在这个代码中,我可以创建html和其他东西。你要么需要一个选择器(数据属性、类等等),要么可以添加一个onclick=“foo”