2010-07-15 121 views
0

当我从select菜单中选择一个值时,如何更改我的textarea背景图像?如何更改文本区域的背景图像

<html> 
<head> 
<title> Your Title </title> 

<script> 
function kool() 
{ 
`enter code here`abc.style.background="img1.bmp" 
} 
</script> 
</head> 
<body> 

<textarea name="abc"> 
1) Make paper airplanes out of the exam. Aim them at the instructor's left nostril. 
2) Bring cheerleaders during an exam. 
</textarea> 

<select id="xyz" onchange="kool()"> 
<option value="A">Image 1</option> 
<option value="B">Image 2</option> 
</select> 

</body> 
</html> 

回答

2

这应该工作:

<textarea id="text"> 
Lorem ipsum dolor sit amet, ... 
</textarea> 

<select onchange="document.getElementById('text').style.backgroundImage = 'url(' + this.value + ')';"> 
<option value="image1.png">Image 1</option> 
<option value="image2.png">Image 2</option> 
</select> 
+0

在这里我指定我的图像路径?? – subanki 2010-07-15 14:20:13

+0

'! – 2010-07-15 14:21:43

+0

哇你的awsome,thanx的朋友,它的作品 – subanki 2010-07-15 14:26:34

0

它应该是:

<script> 
function kool(input) 
{ 
    var el = document.getElementById('abc'); 
    el.style.backgroundImage = 'url(' + input + ')'; 
} 
</script> 
</head> 
<body> 

<textarea name="abc" id="abc"> 
1) Make paper airplanes out of the exam. Aim them at the instructor's left nostril. 
2) Bring cheerleaders during an exam. 
</textarea> 

<select id="xyz" onchange="kool(this.value)"> 
<option value="A.jpg">Image 1</option> 
<option value="B.jpg">Image 2</option> 
</select> 

确保路径是正确的。

+0

'url()'不是JavaScript函数,所以不会工作;) (编辑它为亚) – 2010-07-15 14:21:27

+0

@Douwe M .:感谢哥们,我忘了那些引号:) – Sarfraz 2010-07-15 14:27:56