如何在JavaScript函数中为表分配类或ID?
我必须使用HTML、CSS和JavaScript(因此没有jQuery) 我创建了一个通过API接收JSON数据的表。我有一个通过单击复选框来显示/隐藏每个表行的功能。我已经包含了JavaScript函数,但是我不知道在哪里放置类或id,所以我可以将函数连接到每个表行。通常,我会向如何在JavaScript函数中为表分配类或ID?,javascript,html,mysql,css,json,Javascript,Html,Mysql,Css,Json,我必须使用HTML、CSS和JavaScript(因此没有jQuery) 我创建了一个通过API接收JSON数据的表。我有一个通过单击复选框来显示/隐藏每个表行的功能。我已经包含了JavaScript函数,但是我不知道在哪里放置类或id,所以我可以将函数连接到每个表行。通常,我会向添加一个类,例如:,但在这种情况下,它将不起作用。当我这样做时,代码会中断 我确实在网上搜索了几个小时,但没有找到答案。我不是在寻找完成的代码,而是如何实现这一点的提示/帮助 此表是使用以下方法创建的: 正文{ 背景
添加一个类,例如:
,但在这种情况下,它将不起作用。当我这样做时,代码会中断
我确实在网上搜索了几个小时,但没有找到答案。我不是在寻找完成的代码,而是如何实现这一点的提示/帮助
此表是使用以下方法创建的:
正文{
背景:白色;
}
h1{
颜色:黑色;
字体大小:35px;
文本对齐:居中;
字体系列:“流沙”,无衬线;
}
氢{
字体系列:“流沙”,无衬线;
左边距:3.3em;
}
表,th,td{
边界:无;
边界塌陷:塌陷;
填充:15px;
左侧边缘:5em;
边缘顶部:-25em;
}
表tr:第n个子项(奇数){
背景色:#f1f1;
}
表tr:第n个子项(偶数){
背景色:#ffffff;
}
#形式{
字体系列:“流沙”,无衬线;
左侧边缘:5em;
}
#谷歌地图{
左边距:45em;;
右边距:自动;
}
#chkbox_显示器{
左侧边缘:5em;
边缘顶端:3em;
字体系列:“流沙”,无衬线;
}
.隐藏{
显示:无;
}
天气应用程序
天气预报
请输入以下信息:
输入您的城市:
如何显示值:
公制单位(塞尔西单位/毫米)
英制单位(华氏/英寸)
显示天气的天数:
仅今天
2天
三天
4天
5天
提交
最高温度。
最低温度
降雨量
压力
湿度
风速
函数initAutocomplete(){
weather\u city=new google.maps.places.Autocomplete(
(文件getElementById(“天气城市”),
{类型:['geocode']});
weather_city.addListener(“地点改变”);
}
var经度;
纬度;
函数初始化(arr){
var lon=arr.city.coord.lon;
var lat=arr.city.coord.lat;
var mapProp={
中心:新google.maps.LatLng(lat,lon),
缩放:10,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
var map=new google.maps.map(document.getElementById(“googleMap”),mapProp);
}
函数getJson(请求){
var xmlhttp=new XMLHttpRequest();
open(“GET”,请求,true);
xmlhttp.send();
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
myFunction(xmlhttp.responseText);
}
}
}
函数fetchUrl(){
var form=document.getElementById(“表单”);
var city=form.weather\u city.value;
var值=形式·天气·尺度·值;
变量天数=form.weather\u numberOfDays.value;
变量url=”http://api.openweathermap.org/data/2.5/forecast/daily?q=“+city+”&type=accurate,us&mode=json&appid=a0dd3d46dd5b22c0581030acf10af408&units=“+value+”&cnt=“+days;
getJson(url);
返回false;
}
功能myFunction(响应){
var arr=JSON.parse(响应);
初始化(arr);
var i;
var out=“”;
对于(i=0;i
我建议您使用'knockoutjs'通过返回的Json数据填充表格。就你的目的而言,它真的很合适。它绝对容易学。如果你对这种方法感兴趣,你可以在评论中问我问题。我会尽力帮你解决你的特殊问题
参考:鉴于当前的方法,实现这一点的最简单方法是将
开始标记的HTML从以下内容转换为:
"<tr><td>"
或者,一旦分配了innerHTML
字符串,您可以简单地添加以下行:
// retrieves the collection of <td> elements from within the
// element with the id of 'table', and uses Array.from() to
// convert that collection into an Array.
// Then iterates over the array of elements using
// Array.prototype.forEach():
Array.from( document.querySelectorAll('#table td') ).forEach(function (td) {
// within the anonymous function the 'td' argument is a reference
// to the current array-element of the array over which we're
// iterating.
// here we use the Element.classList API to add the 'example'
// class-name to the existing (if any) class-names of the <td>
// elements:
td.classList.add('example');
});
天气应用程序
天气预报
请输入以下信息:
输入您的城市:
如何显示值:
公制单位(塞尔西单位/毫米)
英制单位(华氏/英寸)
显示天气的天数:
仅今天
2天
三天
4天
5天
.example {
color: #f90;
}
// retrieves the collection of <td> elements from within the
// element with the id of 'table', and uses Array.from() to
// convert that collection into an Array.
// Then iterates over the array of elements using
// Array.prototype.forEach():
Array.from( document.querySelectorAll('#table td') ).forEach(function (td) {
// within the anonymous function the 'td' argument is a reference
// to the current array-element of the array over which we're
// iterating.
// here we use the Element.classList API to add the 'example'
// class-name to the existing (if any) class-names of the <td>
// elements:
td.classList.add('example');
});