使用JavaScript更改用户输入的URL输出
我试图在这里构建一些简单的东西: 用户在输入字段中键入url,例如使用JavaScript更改用户输入的URL输出,javascript,url,Javascript,Url,我试图在这里构建一些简单的东西: 用户在输入字段中键入url,例如http://sharepoint.com/human-resources/usa/Lists/testList/EditForm.aspx?ID=666&Source=http%3A%2F%sharepoint.com 。。点击“提交”,当URL作为链接被吐出时,变为:https://sharepointusa.com/en-us/human-resources/usa/Lists/testList/EditForm.aspx?
http://sharepoint.com/human-resources/usa/Lists/testList/EditForm.aspx?ID=666&Source=http%3A%2F%sharepoint.com
。。点击“提交”,当URL作为链接被吐出时,变为:https://sharepointusa.com/en-us/human-resources/usa/Lists/testList/EditForm.aspx?ID=666&Source=http%3A%2F%sharepoint.com
我一直试图将整个URL吐出,但没有成功,丢失了参数,所以我需要一种新的方法,什么是简单的普通javascript,可以替换并保留URL的其余部分
谢谢
编辑:2个很好的答案,谢谢,我将第一个答案改编为我的原始代码,同时我将第二个答案与之进行比较
<a href="" id="link"></a><br>
<input type="text" id="userInput" value="Enter your text here"><br>
<input type="button" onclick="changeText2()" value="change text">
<script>
function changeText2()
{
var input=document.getElementById('userInput').value;//gets the value entered by user
const updatedUrl = input.replace('http://sharepoint.com/', 'https://sharepointusa.com/en-us/');
document.getElementById("link").href = updatedUrl;
document.getElementById("link").innerHTML = updatedUrl;
}
</script>
函数changeText2()
{
var input=document.getElementById('userInput').value;//获取用户输入的值
const updatedUrl=input.replace('http://sharepoint.com/', 'https://sharepointusa.com/en-us/');
document.getElementById(“link”).href=updateUrl;
document.getElementById(“link”).innerHTML=updateUrl;
}
如果您有一个包含完整原始url的变量
const url = 'http://sharepoint.com/human-resources/usa/Lists/testList/EditForm.aspx?ID=666&Source=http%3A%2F%sharepoint.com';
那你就做吧
const updatedUrl = url.replace('http://sharepoint.com/', 'https://sharepointusa.com/en-us/');
updateURL将满足您的要求。如果您有一个包含完整原始url的变量
const url = 'http://sharepoint.com/human-resources/usa/Lists/testList/EditForm.aspx?ID=666&Source=http%3A%2F%sharepoint.com';
那你就做吧
const updatedUrl = url.replace('http://sharepoint.com/', 'https://sharepointusa.com/en-us/');
更新后的URL将满足您的要求。它就在我面前!无论如何,这是一种更高级的表示方式,说明了如何直接从输入字段进行更改
<!DOCTYPE html>
<html>
<body>
<input id="demo" value="http://sharepoint.com/human-resources/usa/Lists/testList/EditForm.aspx?ID=666&Source=http%3A%2F%sharepoint.com">
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = document.getElementById("demo").value;
var changed = str.replace("sharepoint", "sharepointusa");
document.getElementById("demo").value = changed;
}
</script>
</body>
</html>
试试看
函数myFunction(){
var str=document.getElementById(“demo”).value;
var changed=str.replace(“sharepoint”、“sharepointusa”);
document.getElementById(“demo”).value=changed;
}
它就在我面前!无论如何,这是一种更高级的表示方式,说明了如何直接从输入字段进行更改
<!DOCTYPE html>
<html>
<body>
<input id="demo" value="http://sharepoint.com/human-resources/usa/Lists/testList/EditForm.aspx?ID=666&Source=http%3A%2F%sharepoint.com">
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = document.getElementById("demo").value;
var changed = str.replace("sharepoint", "sharepointusa");
document.getElementById("demo").value = changed;
}
</script>
</body>
</html>
试试看
函数myFunction(){
var str=document.getElementById(“demo”).value;
var changed=str.replace(“sharepoint”、“sharepointusa”);
document.getElementById(“demo”).value=changed;
}
对不起,我去把我正在使用的代码放进去了-我马上得到了答案,所以我只需粘贴正确的代码。您需要在问题中提出一个明确的问题。不要把答案放在那里。如果您还没有这样做,问题将被关闭,因为它不会为社区提供太多价值。谢谢。对不起,我去把我正在使用的代码放进去了-我马上得到了答案,所以我只是粘贴了正确的代码。你需要在你的问题中提出一个明确的问题。不要把答案放在那里。如果您还没有这样做,问题将被关闭,因为它不会为社区提供太多价值。谢谢