3 回答
![?](http://img1.sycdn.imooc.com/54586453000163bd02200220-100-100.jpg)
TA贡献1852条经验 获得超7个赞
我在几个地方稍微改变了你的代码
给event和event.preventDefault()findSharp 函数从 html 中删除返回语句(更多在这里)
向 findURLAA 和 findURLB 添加参数(并修复第二个函数名称中的类型错误)以省略重复读取并消除函数与全局/html 变量的耦合
临时注释 findURL-A/B 内容 - 可以取消注释
function findSharp(event) {
var path = pathID.value;
if (path[0] == '#') {
findURLA(path)
} else {
findURLB(path)
}
event.preventDefault(); // not change location by form
}
function findURLA(path) {
var hashIdReplace = path.replace('#', '');
console.log('A:', hashIdReplace)
// window.location.href = 'http://localhost:8181/activity-2/' + '?' + 'hashtag/' + hashIdReplace;
}
function findURLB(path) {
console.log('B:', path)
//window.location.href = 'http://localhost:8181/';
//document.write("I AM URL B");
}
<form>
<input id="pathID" placeholder="Search ..." style="height:40px; line-height:40px; padding:0 15px;">
<input onclick="findSharp(event)" type="submit" value="Search">
</form>
![?](http://img1.sycdn.imooc.com/533e4d5b0001d57502200203-100-100.jpg)
TA贡献1850条经验 获得超11个赞
你可以试试这个。
function findSharp(){
var stringName =
document.getElementById("pathID").value;
if(stringName[0] == '#'){
findURLA(stringName.slice(1,))
}
else{
findURLB(stringName)
}
}
function findURLA(hashId){
window.location.href = 'http://localhost:8181/activity-2/' + '?' + 'hashtag/' + hashId;
}
function findURLB(hashId){
window.location.href = 'http://localhost:8181/';
document.write("I AM URL B");
}
添加回答
举报