将输入文本传递到URL以呈现新的iframe页
我想将输入到输入框中的文本传递到URL的中间,该URL将根据用户“搜索”呈现嵌入式iframe。单词之间的空格必须自动转换为“%20”或“+”。将输入文本传递到URL以呈现新的iframe页
在这里看到的想什么,我实现一个例子:
Enter a keyword or phrase into the box below to see if it's trending.
<br>
<input placeholder="***input***"></input>
<iframe src="http://www.google.com/trends/fetchComponent?hl=en-US&q=***INPUT***&cid=TIMESERIES_GRAPH_0&export=5&w=500&h=300"></iframe>
var timeout;
var input = document.querySelector('input');
var iframe = document.querySelector('iframe');
input.addEventListener('input', function(e){
if(timeout !== undefined)
clearTimeout(timeout);
timeout = setTimeout(function(){
iframe.src = 'http://www.google.com/trends/fetchComponent?hl=en-US&q='+ input.value +'&cid=TIMESERIES_GRAPH_0&export=5&w=500&h=300';
}, 1000);
});
iframe{
width:100%;
height:400px;
}
input{
margin:40px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input placeholder="***input***"></input>
<iframe src="http://www.google.com/trends/fetchComponent?hl=en-US&q=***INPUT***&cid=TIMESERIES_GRAPH_0&export=5&w=500&h=300"></iframe>
它可能会更好,只是有一个按钮,虽然,每次您在输入框上进行更改时,输入事件都会触发。
var input = document.querySelector('input');
var iframe = document.querySelector('iframe');
var button = document.querySelector('button');
button.addEventListener('click', function(e){
iframe.src = 'http://www.google.com/trends/fetchComponent?hl=en-US&q='+ input.value +'&cid=TIMESERIES_GRAPH_0&export=5&w=500&h=300';
});
iframe{
width:100%;
height:400px;
}
input{
margin:40px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input placeholder="***input***"></input><button>go</button>
<iframe src="http://www.google.com/trends/fetchComponent?hl=en-US&q=***INPUT***&cid=TIMESERIES_GRAPH_0&export=5&w=500&h=300"></iframe>
FWIW,这个例子中没有任何东西没有jQuery没有办法很容易做到。 –
是的,我意识到这一点。我从一开始就想到'jQuery'。我会让它变成香草JS。 – MinusFour
有没有什么方法可以写出来,以显示按钮的工作方式?我认为你是正确的,从UX的角度来看这会更好。 –
你是什么实际问题?你试过什么了?你正在谈论你想要的,无处我看到这是我的问题,这是导致问题。有关如何提问的解释,请参见[如何提问](http://*.com/help/how-to-ask)。 – DragonSamu