将 url 中的一个字符串替换为另一个字符串
Replacing a string in url with another string
用filmot替换imgur,
输入框 - https://i.stack.imgur.com/Uguvn.jpg
"提交"按钮
在新标签页中单击提交后 http://i.filmot.com/abcde.jpg必须打开。
<html>
<head>
<title>input</title>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
function myFunction() {
var res = str1.replace("imgur", "filmot");
var win = window.open(res, '_blank');
win.focus();
});
</script>
</head>
<body>
<form>
<input type=”text” id=”str1” />
<button onclick="myFunction()">Click</button>
</form>
</body>
</html>
返回代码不起作用。请指教。
谢谢:)
编辑:
下面是解决方案代码:)
<html>
<head>
<title>input</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
function myFunction() {
var str1 = document.getElementById('str1').value;
var res = str1.replace("imgur", "filmot");
document.getElementById('str1').value = res;
var win = window.open(res, '_blank');
win.focus();
}
</script>
</head>
<body>
<input type="text" id="str1" />
<button onclick="myFunction()">Click</button>
</body>
</html>
试试这个:
<script>
function myFunction() {
var str1 = document.getElementById('str1').value; // add this
var res = str1.replace("imgur", "filmot");
var win = window.open(res, '_blank');
win.focus();
} // <-----do a proper function closing.
</script>
需要提到的另一件事可能是您使用的引号type=”text” id=”str1”
,它们不是正确的引号。这应该改为:
type="text" id="str1"
查看演示:
function myFunction() {
var str1 = document.getElementById('str1').value;
var res = str1.replace("imgur", "filmot");
document.getElementById('str1').value = res;
//window.open(res, '_blank');
//win.focus();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="str1" value="http://i.imgur.com/abcde.jpg" />
<button onclick="myFunction()">Click</button>
更改此代码。
var res = str1.replace("imgur", "filmot");
自
var res = $("#str1").val().replace("imgur", "filmot");
您的代码没有从文本框中获取值......您只是使用了对文本框的引用。
更新
在函数结束时,将});
更改为}
参考打开新窗口教程:
要打开一个新窗口,您需要使用另一个现成的 JavaScript 函数。下面是它的样子:
window.open('url to open','window name','attribute1,attribute2')
我已经编辑了函数(添加了窗口名称'),它工作正常,替换了 url a,然后在新选项卡中打开它:
function myFunction() {
var str1 = document.getElementById('str1').value;
var res = str1.replace("imgur", "filmot");
window.open(res, 'new window', '_blank');
}
这是工作演示。
请尝试以下操作:
<html>
<head>
<title>input</title>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
function myFunction() {
var newString=str1.value;
var res = newString.replace("imgur", "filmot");
var win = window.open(res, '_blank');
win.focus();
};
</script>
</head>
<body>
<form>
<input type="text" id="str1" value=""/>
<button onclick="myFunction();">Click</button>
</form>
</body>
</html>
相关文章:
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- 制作一个regex来验证只有一个数字的字符串
- 将html元素插入到文本字符串中,以匹配另一个html字符串
- NodeJS在一个较大的字符串中获取一个字符串的索引
- jquery从2个json字符串构建一个复选框表单
- 在Javascript中将一个值和字符串数组转换为if语句
- Javascript:用while循环在给定的count参数中重复一个字符串
- 如何使用另一个字符串作为模板从字符串中提取数据
- 用jQuery将字符串从一个类替换为另一个类
- 使用javascript或angularjs特定过滤器搜索字符串中第一个img标记的json值
- 如何检查一个字符串的所有字符是否都存在于另一个字符串中
- JavaScript:如何创建一个具有字符串名称的对象
- 如何从 javascript 中可能具有一个或多个图像的字符串中仅获取图像名称
- 在不使用循环的情况下,从一个数据库字符串值向javascript数组添加多个对象
- 从表单中动态生成一个字符串,传递给通过AJAX加载的PHP文件
- 当我JSON.stringfy(对象)时,我得到一个疯狂的字符串作为值
- 我可以使用JS一次使用JSON数组中的一个字符串吗
- 使用javascript替换字符串中除第一个和最后一个之外的所有字符
- 获取一个对象´s属性的名称使用字符串
- 删除一个“;从javascript中的字符串末尾