2010-07-15 42 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