【问题标题】:How to change the background image of a textarea如何更改文本区域的背景图像
【发布时间】:2010-07-15 14:14:48
【问题描述】:

当我从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>

【问题讨论】:

    标签: javascript html coding-style


    【解决方案1】:

    这应该可行:

    <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>
    

    【讨论】:

    • 我在哪里指定我的图像的路径??
    • value="" 中为&lt;option /&gt;
    • 在你的代码中我如何跟踪我的文本区域,我的意思是我的文本区域实际上在 iframe 中
    • 我也给了+1,如何设置我的textarea路径
    • 您的文本区域在iframe 内?试试这个:window.frames['dork'].getElementById('id').style.etc,将id 替换为iframetextarea 的实际id 属性。
    【解决方案2】:

    应该是:

    <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>
    

    确保路径正确。

    【讨论】:

    • url() 不是 JavaScript 函数,所以它不起作用;)(为你编辑)
    • @Douwe M.:谢谢哥们,我忘记了那些话 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2012-12-11
    • 1970-01-01
    相关资源
    最近更新 更多