- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面的代码是我一直在研究的代码。当我根据产品的计算分数对产品进行评级时,我认为我的逻辑是有缺陷的,这就是我混淆的地方。而且我也不确定如何根据分数创建评级图像。数组中的第一列是得分值,第二列是产品 ID,第三列是产品名称,第四列是品牌网站的链接。我想在第五列中输出一张反射(reflect)产品得分的图像。例如,如果我根据分数对产品进行排序,并且希望首先列出分数最高的产品,那么它应该是具有 5 星图像的产品 [1]、具有 4 星图像的产品 [3]、 [0] 具有 3 星图像,产品[4] 具有 2 星图像,产品[2] 具有 1 星图像。 注意:由于某种原因, language="JavaScript"对我有用,而 type="text/javascript"则不然。如果有人能为这段代码提供任何指导,我将真诚地感激不已。预先非常感谢您:)
HTML:
<table id="table">
<tr id="tbody">
<th>Score</th>
<th>ID</th>
<th>Name</th>
<th>Website</th>
<th>Rating</th>
</tr>
</table>
JavaScript:
var products = new Array(5);
products[0] = [26, 4859, "Panasonic TV", "http://www.panasonic.com"];
products[1] = [37, 4762, "Sony TV", "http://www.sony.com"];
products[2] = [9, 4899, "LG TV", "http://www.lg.com"];
products[3] = [34, 5001, "Samsung TV", "http://www.samsung.com"];
products[4] = [22, 3425, "Vizio TV", "http://www.vizio.com"];
function Comparator(a,b) {
if (if (a[0] > b[0]) return -1;
if (a[0] < b[0]) return 1;
return 0;
}
var productsSorted = products.sort(Comparator);
for (i=0; i<products.length, i++) {
if (products[0] === 37) {
document.getElementById("IMG5");
} else if (products[0] === 34) {
document.getElementById("IMG4");
} else if (products[0] === 26) {
document.getElementById("IMG3");
} else if (products[0] === 22) {
document.getElementById("IMG2");
} else {
document.getElementById("IMG1");
};
table.appendChild(tbody);
productsSorted.forEach(function(item) {
var row = document.createElement("tr");
var score = document.createElement("td");
score.textContent = item[0];
var ID= document.createElement("td");
ID.textContent = item[1];
var name = document.createElement("td");
name.textContext = item[2];
var link_td = document.createElement("td");
var link = document.createElement("a");
link.textContent = item[3]
link.href = item[3]
link_td.appendChild(link);
var rating = new Array(5);
var rating_td = document.createElement("td");
var rating[0] = document.createElement("IMG5");
rating.setAttribute("src","http://dkcoin8.com/images/five-star-clipart-4.png");
var rating[1] = document.createElement("IMG4");
rating.setAttribute("src","http://aliviogroup.com.au/wp-content/uploads/2016/04/four-stars.jpg");
var rating[2] = document.createElement("IMG3");
rating.setAttribute("src","https://waytoomuchtelevision.files.wordpress.com/2011/01/3star.jpg");
var rating[3] = document.createElement("IMG2");
rating.setAttribute("src","https://authorjanedoe.files.wordpress.com/2012/07/2star.jpg");
var rating[4] = document.createElement("IMG1");
rating.setAttribute("src","http://clipart.printcolorcraft.com/wp-content/uploads/stars/smooth%20star.jpg");
rating_td.appendChild(rating);
row.appendChild(score);
row.appendChild(ID);
row.appendChild(name);
row.appendChild(link);
row.appendChild(rating);
table.appendChild(row);
});
最佳答案
创建一个星星的 png,其中背景为白色,星星是透明的。将其中 5 个并排放入 div(或您的情况下的 td)中。创建另一个黄色矩形图像。将星星 div/td 的背景设置为黄色矩形。将 div/td 的背景大小参数设置为基于分数的百分比。因此,如果平均分数为 70%,则 70% 的背景将为黄色,从而产生 3 个黄色星星、一颗部分填充黄色的星星和一颗未填充的星星。
说到分数...它们似乎是分数的总和。如果是这种情况,那么您应该计算平均值而不是总和并将其放在第一列中,或者包括评级数量的计数,以便您可以计算平均分数。例如,如果分数 9 是 4/5 和 5/5 的结果,那么您的实际分数是 9/10 或 90%。
function createRows() {
var products = new Array(5);
products[0] = [84, 4859, "Panasonic TV", "http://www.panasonic.com"];
products[1] = [73, 4762, "Sony TV", "http://www.sony.com"];
products[2] = [90, 4899, "LG TV", "http://www.lg.com"];
products[3] = [88, 5001, "Samsung TV", "http://www.samsung.com"];
products[4] = [44, 3425, "Vizio TV", "http://www.vizio.com"];
var items = document.getElementById("items");
Array.prototype.forEach.call(products, function(prod) {
var row = document.createElement("div");
row.className = "row";
var fields = new Array(4);
var field_num = 0;
Array.prototype.forEach.call(prod, function(field) {
fields[field_num] = document.createElement("div");
fields[field_num].className = "field";
if (field_num > 0) {
fields[field_num].innerHTML = prod[field_num];
fields[field_num].id = "field" + field_num;
}
row.append(fields[field_num]);
field_num++;
});
fields[0].id = "rating";
fields[0].style.backgroundSize = prod[0] + "% 50px";
for (i=0; i < 5; i++) {
var star = document.createElement("img");
star.src = "https://i.imgur.com/jxqW2CC.png";
fields[0].append(star);
}
items.append(row);
});
}
createRows();
.field {
display: inline-block;
line-height: 19px;
vertical-align: top;
padding: 3px 5px;
}
#field1 {
width: 50px;
border-right: 1px solid black;
height: 100%;
}
#field2 {
width: 100px;
border-right: 1px solid black;
}
.row {
border: 1px solid black;
margin: 5px;
font: 14px Arial, sans-serif;
background: white;
}
#rating {
height: 25px;
width: 125px;
padding: 0px;
border-right: 1px solid black;
background: #ccc url(https://upload.wikimedia.org/wikipedia/commons/1/15/Orange_rectangle.svg) no-repeat;
}
#rating img {
height: 25px;
padding: 0px;
}
<div id="items">
</div>
关于javascript - 有没有办法在 JavaScript 数组中创建评级图像(5 星、4 星、3 星等...)并将其全部输出为 HTML 中的表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45749526/
我正在使用 MySQL 和 PHP 为 Flash 游戏构建数据层。检索关卡列表非常简单,但我在尝试获取关卡的平均评分及其指针信息时遇到了障碍。这是一个示例数据集: 级别表: level_id | l
阅读 schema.org 之后我写了一个小的“丰富代码片段”或者它所谓的什么。我们今天被谷歌找到,但没有显示星级和评级。也许有人对此有经验并且知道事情是如何完成的?它仅取决于微数据还是取决于 中的
有没有一种方法(或 API)可以使远程 SMF 站点排队以获取特定时间范围内的热门/热门主题,比如过去 24 小时?如果没有,关于我如何跟踪它有什么建议吗? 最佳答案 不确定这是否可行,但我知道 SM
有谁知道如何使用 Foursquare API 获取 field 的分数/评级(例如 9.0/10)? 我正在通过无用户访问进行连接。 https://developer.foursquare.com
我正在使用 Sonar 来分析一组相关的项目。 我正在使用 SQALE 评级来证明重构的必要性 我的问题是 SQALE 到技术债务比率映射背后的逻辑是什么? 为什么 SQALE A 评级是技术债务,范
我有一份 FIFA 19 两名球员之间的比赛列表,我希望根据所参加的比赛使用该数据更新我在这两名球员之间的 ELO 评分。我正在尝试使用此数据不断更新最初从 1000 开始的 ELO 评级。 我尝
我无法显示评级元素。它根本就没有出现。所有其他事情都很好 我已经正确地链接了这些: 语义用户界面 CSS ( http://semantic-ui.com/dist/semantic.min.css
我正在开发一个 Java 应用程序,其中涉及使用 Foursquare 来搜索 field 。该应用程序预计也会对场馆进行评级。除了场馆的收视率之外,我已经做了所有我需要做的事情。我阅读了利用 fou
由于我公司的一位客户要求标记图像上的 alt 属性,我遇到了 google maps API 的问题。 我在谷歌官方文档中找不到任何内容(即:https://developers.google.com
有人可以给我一个 onRate() 的例子吗? Semantic UI 中评级小部件的回调函数 ( http://semantic-ui.com/modules/rating.html#/settin
我有四个主表- 1.user(user_id, name,...) 2. post(post_id, description, ...) 3.comment(comment_id,user_id,co
我已登录并获取身份验证 token ,但当我单击“赞”按钮时,YouTube 评级不起作用。我还需要做什么才能让它发挥作用?如果我在网络浏览器中使用该网址,它会要求授权并执行,然后它就可以工作。但从应
我正在使用我在网上找到的一些评级系统 CSS。目前我能够让它工作,但是当显示多个评级时会出现问题。 只有最后一个分级类有效。 .rate { float: left; pointer-even
我安装了一个应用程序 django- ratings。我想要syncdb,但这是我的错误: djangoratings.vote: 'user' defines a relation with the
每个人都喜欢在他们的网站上显示 Digg/Tweet/Like 徽章,而 Disqus Comment System开始接管。 (来源:disqus.com) (来源:digg.com) 从开发人员的
我有一个 Bootstrap Rating 组件,我必须使用 on-leave 方法来调用我的 javascript 函数。除了 IE 之外,所有其他浏览器也在单击事件上执行 on-leave 函数。
我正在用 Rating Bootstrap 做一个 MVC 应用程序。 我也定义了这个...为了知道点击了多少开始。 $(document).ready(function () {
我想按人口统计(性别、年龄组)收集 IMDb 评级详细信息。 当我尝试在 imdbpy 中使用 get_movie_vote_details 模块时,我的输出为空。这是我的代码: import im
我正在尝试使用 PHP 和 SoapClient 来利用 UPS Ratings 网络服务。我找到了一个名为 WSDLInterpreter 的好工具来创建用于创建服务请求的起点库,但无论我尝试什么,
我正在尝试向现有表格添加“评级”系统(即 1 星、2 星或差、一般、好、优秀等)。有谁知道在 .Net 或免费的第 3 方控件中通过良好的 UX 实现美观的方法? 谢谢 最佳答案 检查 ASP.NET
我是一名优秀的程序员,十分优秀!