【发布时间】:2020-10-17 05:26:07
【问题描述】:
如何根据表格单元格值更改 img src 标签值?
我有以下代码:
<table class="table table-bordered table-hover table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">Firstname</th>
<th scope="col">Lastname</th>
<th scope="col">Email</th>
</tr>
</thead>
<tbody id="myTable">
{% for user in users %}
<tr>
<td onmouseover="imageAppear('place-holder-1')" onmouseout="imageDisappear('place-holder-1')">{{ user.fname }}<img src="{{ url_for('static', filename='images/name_of_pic.jpg') }}" id="place-holder-1" style="zindex: 100; height: 400px; position: absolute; visibility: hidden;"/></td>
<td>{{ user.lname }}</td>
<td>{{ user.email }}</td>
</tr>
{% endfor %}
</tbody>
</table>
这是一个烧瓶应用程序,我从数据库中提取数据并填写表格。我写了一个 javascript 函数,当鼠标在名字上时会显示每个用户的照片,但现在它为每个用户显示相同的图片。
<script>
function imageAppear(id) {
document.getElementById(id).style.visibility = "visible";}
function imageDisappear(id) {
document.getElementById(id).style.visibility = "hidden";}
</script>
所以现在我想从 img src 动态构建 url,这样我就可以看到每个用户的图片。 images 文件夹中的图片名称格式为“user.fname.jpg”。那么如何在url中动态设置user.fname呢? user.fname 将是表中的第一个 td。
【问题讨论】:
标签: javascript html src