javascript - 如何在 <src> 中调用 javascript 函数?

标签 javascript html

<分区>

我还需要知道如何在“src”中调用 javascript 函数

<input type=text>

我正在使用一个返回查询字符串参数的函数

function GetUrlValue(VarSearch) {
            var SearchString = window.location.search.substring(1);
            var VariableArray = SearchString.split('&');
            for (var i = 0; i < VariableArray.length; i++) {
                var KeyValuePair = VariableArray[i].split('=');
                if (KeyValuePair[0] == VarSearch) {
                    return KeyValuePair[1];
                }
            }
        }

如果:

"www.testweb.com?a=521&b=http://demo.sacredpixel.com/redsky/wp/modern/wp-content/uploads/2013/05/gd-1.jpg"

所以它会返回

alert(GetUrlValue('a')); ===> 521

但我需要的是在 img 中调用此函数并输入类型“文本” 标签……比如:

<iframe src="" id="well" width="100%" height="100%" />
<input type="text" value="" />

如何调用上面两个函数?

最佳答案

我认为您需要这种类型的工作。请查看并告诉我。

HTML:-

<img id="myimg" src="" />  
<input type="text" id="inputfield" value="">  

JS:-

 function GetUrlValue(VarSearch) {  
        var SearchString = window.location.search.substring(1);  
        var VariableArray = SearchString.split('&');  
        for (var i = 0; i < VariableArray.length; i++) {  
            var KeyValuePair = VariableArray[i].split('=');  
            if (KeyValuePair[0] == VarSearch) {  
                return KeyValuePair[1];  
            }  
        }  
    }  

    document.getElementById('myimg').src = GetUrlValue('b');  
    //"http://www.w3schools.com/images/w3html.gif";  
    document.getElementById('inputfield').value = GetUrlValue('a');  

托管文件并尝试这个 url:- "www.testweb.com?a=521&b= http://demo.sacredpixel.com/redsky/wp/modern/wp-content/uploads/2013/05/gd-1.jpg "

它对我有用。希望对你有帮助。

关于javascript - 如何在 <src> 中调用 javascript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21746456/

相关文章:

javascript - Skitter jQuery Slideshow - 想要设置时间间隔

javascript - 获取父Div,然后获取里面的元素

javascript - 循环遍历对象中的段落。如何?

javascript - 您可以滚动到相对定位的元素的 html 中的 anchor 吗?

javascript - discord.js v14 中的 setPresence 事件类型只能设置为 "PLAYING"

javascript - 使用vue element ui区分用户输入和数据更改

javascript - 单击按钮更改背景颜色

javascript - 如何更改网页中下拉滚动条的初始位置?

javascript - 如何使用 javascript 添加特定对象?

html - 更改 Div 上的边距不会影响?