使用Javascript函数返回表元素中的值
我需要将此结果显示为表格的背景图像。我已经在javascript的底部添加了一个函数,但无法让它通过。我删除了它的功能,因为它不工作 这与现在一样有效,但不会将图像置于背景中 谢谢使用Javascript函数返回表元素中的值,javascript,html,css,image,Javascript,Html,Css,Image,我需要将此结果显示为表格的背景图像。我已经在javascript的底部添加了一个函数,但无法让它通过。我删除了它的功能,因为它不工作 这与现在一样有效,但不会将图像置于背景中 谢谢 <HTML> <head><base href="http://localhost:85/shop/" target="_blank"></head> <!--.<style> divcurve { border:1px solid; borde
<HTML>
<head><base href="http://localhost:85/shop/" target="_blank"></head>
<!--.<style>
divcurve
{
border:1px solid;
border-radius:5px;
}
</style>
-->
<style>
IMG {BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none}
</style>
<script type="text/javascript">
//Javascript Created by Computerhope http://www.computerhope.com
//store the quotations in arrays
var images = [],
index = 0;
images[0] = "<a href = '/shop/category.aspx/about/711/'><img src='/images/opc.png' alt='Tech Support' height='140' width='165'></a>";
images[1] = "<a href = '/shop/category.aspx/about/711/'><img src='/images/opd.png' alt='Barcode Help' height='140' width='165'></a>";
images[2] = "<a href = '/shop/category.aspx/about/711/'><img src='/images/opj.png' alt='Barcode Help' height='140' width='165'></a>";
images[3] = "<a href = '/shop/category.aspx/about/711/'><img src='/images/opg.png' alt='Barcode Help' height='140' width='165'></a>";
images[4] = "<a href = '/shop/category.aspx/about/711/'><img src='/images/opjb.png' alt='Barcode Help' height='140' width='165'></a>";
index = Math.floor(Math.random() * images.length);
document.write(images[index]);
//done
</script>
<body>
<table>
<tr><td width="20%>
<img src="/images/phone.gif">
</td>
<td width="70%">
</td>
</tr>
</table>
</body>
</html>
IMG{BORDER-RIGHT:medium-none;BORDER-TOP:medium-none;BORDER-LEFT:medium-none;BORDER-BOTTOM:medium-none}
//Computerhope创建的Javascripthttp://www.computerhope.com
//将报价存储在数组中
var images=[],
指数=0;
图像[0]=“”;
图像[1]=“”;
图像[2]=“”;
图像[3]=“”;
图像[4]=“”;
index=Math.floor(Math.random()*images.length);
文件。写入(图像[索引]);
//完成
我想我知道你的意思,我可能错了
如果要从包含HTML字符串的数组中选择图像,而HTML字符串恰好也包含图像标记,则要对表的背景图像使用src
属性值
您可以使用正则表达式在for循环中执行此操作,或者首先将数组连接到字符串,但正如Adam Botley所提到的,将每个URL存储在数组中,这将使您的生活更加轻松
看起来你的代码来自一个教程,所以如果你真的想做的就是为表格随机选择一个背景图像,那就更容易了
这是你能做的。创建URL数组。
然后,您需要计算一个随机数以用于密钥。
A是介于0和1之间的浮点数,因此将其乘以数组中URL的数量。返回参数的整数
然后需要对table元素的引用。
要使其真正起作用,必须在HTML中的表中添加一个id。
现在应该可以了,让我知道 问题不清楚。你能对此进行修改吗?不知道你在问什么。尝试将每个图像的url存储在数组中,然后将其指定为背景图像的url
。我感谢你的回答和示例。我很少使用JavaScript,所以即使是简单的东西也很难。好的,我试过上面提到的代码,但是在后台没有结果。我甚至使用了document.write(URL[index]);试着把img写出来,结果也没用。我还仔细检查了图像中的字符串,它确实有效。var url=['images/opc.png','images/opc.png'];var index=Math.floor(Math.random()*images.length);var theTableElement=document.getElementById('theTable');tableElement.style.backgroundImage=URL[索引];测试查看图像是否在后面
如果这样做有效,应该在上面的底部图像。问题是,我给你的内容未经测试:)这里是一个“是”,它似乎由于某种原因无法在本地工作。你的榜样很好。我当然添加了一个打字错误,这也是一个问题。我正在使用带有插件编辑器的记事本+。你会推荐一个好的免费或接近免费的JavaScript编辑器吗@本是我选择的武器。试着获得测试版。它不是免费的,只是偶尔要一把钥匙。
var urls = ['http://lorempixel.com/200/200', 'http://lorempixel.com/200/200'];
var index = Math.floor(Math.random() * urls.length);
var theTableElement = document.getElementById('theTableElement');
<table id="theTableElement">
<tr>
<td>hello</td>
<td>there</td>
</tr>
<tr>
<td>you</td>
<td>person :)</td>
</tr>
</table>
theTableElement.style.background = 'url(' + urls[index] + ')';