javascript - 使用 jquery,如何使用 img src="javascript :? 用匹配的图片填充我的表行

标签 javascript jquery image

假设我有一个表格,如下所示,我正在尝试为每一行填充许多 jpeg,例如 BillSmith.jpgNancyClinton.jpg .

用图片填充的示例表:

纽约 |史密斯,比尔 (试图将 'BillSmith.jpg' 放在这里)
纽约 |克林顿,南希 (试图将 'NancyClinton.jpg' 放在这里)

我很困惑在这部分之后要放什么<img src="javascript:在下面的代码片段的第二行到最后一行中。

$("#html_table tbody td:nth-child(2)").each(function(){
        var the_name = $(this).html();

        // Creates the string of file names ( BillSmith.jpg ) matching the file names of jpeg pics

        var nameList = the_name.split(",");
        var firstLast = nameList[1] + nameList[0];
        var source_foto = "staff_jpegs/"+firstLast+".jpg";

        $(this).html( the_name +'<br><div class="fotos" style="text-align:center"><img src="javascript:source_foto" height="80" width="80"/></div>');
        });

上面的代码不起作用,因为我假设函数而不是变量只能在 javascript: 之后.

寻找一种简单的方法,或多或少使用这种结构将许多照片放入相应的表行中。

最佳答案

$("#html_table tbody td:nth-child(2)").each(function(){
        var the_name = $(this).html();

        // Creates the string of file names ( BillSmith.jpg ) matching the file names of jpeg pics

        var nameList = the_name.split(", ");
        var firstLast = nameList[1] + nameList[0];
        var source_foto = "staff_jpegs/"+firstLast+".jpg";

        $(this).html( the_name +'<br><div class="fotos" style="text-align:center"><img src="'+source_foto+'" height="80" width="80"/></div>');
        });

您不需要 JavaScript:您可以只使用变量名称。另外,你的分割应该在“,”上,而不仅仅是“,”

关于javascript - 使用 jquery,如何使用 img src="javascript :? 用匹配的图片填充我的表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4308465/

相关文章:

javascript - Node.js Web 应用程序中 MySQL 查询的延迟加载

php - 无法在 HTML 中正确使用 <img src =""onclick ='window.open(' anotherpage.html' ,'_blank");'/> 属性

html - 预加载背景图像和 304 响应

javascript - react native : Image doesn't show in the view

javascript - 如何从列表中生成随机值及其结果?

JavaScript Date getTime() 不一致

java - 将 JSON 发送并解析到 spring Controller ?

javascript - Liferay + 分机 Js : Setting up Layout

javascript - node.js 需要来自父文件夹

javascript - HTML 通过点击编辑按钮使 Div 可编辑