我试图创建一个小应用程序,允许我在一些带有lang代码的复选框之间进行选择,然后单击一个按钮,打开带有更改lang代码的URL的新选项卡。
网址格式:https://example.com/ zh-CN /example.html
我想要改变的是我们。我在这里有几个问题:
我知道如何搜索字符串,但有一种方法可以替换它,避免了很多 if 语句?因为网络可以有en-US代码,也可能有ko-KR或es-ES等等。
我想为每个复选框(已选中)打开一个新标签,其中包含所选的lang-code。
那么,有一种简单的方法可以做到这一点吗?
HTML:
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="container">
<div class="info-container">
<p>URL:</p>
<input type="text" id="URL">
<input type="button" id="opentab-button" value="Go!">
</div>
<div class="langcode-container">
<input type="checkbox" name="lang-check" value="ko-kr"> KOR<br>
<input type="checkbox" value="lang-check" value="nl-NL"> NLD<br>
<input type="checkbox" value="lang-check"> PTB<br>
<input type="checkbox" value="lang-check"> PTG<br>
<input type="checkbox" value="lang-check"> CHT<br>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="js/index.js"></script>
</body>
</html>
JavaScript的:
document.getElementById("opentab-button").addEventListener("click", openTabs);
function openTabs() {
var str = document.getElementById("URL").value;
var checkQua = $(":checkbox:checked").length;
for (i = 1; i <= checkQua; i++) {
var res = str.replace("en-us", "ko-kr");
window.open(res,'_blank');
}
}
答案 0 :(得分:2)
我知道如何搜索字符串,但是有一种方法可以替换它,避免了大量的if语句?因为网络可以有en-US代码,也可能有ko-KR或es-ES等等。
将替换值保留在复选框“var output = HtmlService.createTemplateFromFile('index').evaluate();
output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
”属性中,就像前两个value
已经完成的那样。然后,代替<input>
,执行str.replace("en-us", "ko-kr")
。
str.replace("en-us", checkbox.value)
document.getElementById("opentab-button").addEventListener("click", openTabs);
function openTabs() {
var str = document.getElementById("URL").value;
var checkQua = document.querySelectorAll('input[name=lang-check]:checked');
checkQua.forEach(function(checkbox) {
var res = str.replace('en-us', checkbox.value);
console.log('Opening', res);
window.open(res, '_blank');
});
}
请注意,上面的内容实际上并没有打开任何窗口,因为Stack Overflow已禁用片段中的弹出窗口。 (看看你的控制台,看看权限错误。)
答案 1 :(得分:1)
我认为这可以帮到你
$(function(){
$("#opentab-button").on("click", openTabs);
function openTabs() {
var str = document.getElementById("URL").value;
var data = getLocation(str);
// passing by each checkbox
$("[type='checkbox']:checked").each(function(){
var path = data.pathname.split('/');
// Replacing the second item (the language) for the value in the checkbox
path.splice(1,1,$(this).val());
// Replacing the old path of url by the new path
var newPath = data.full.replace(data.pathname,path.join('/'));
//window.open(newPath,'_blank');
alert(newPath);
});
}
function getLocation(url) {
var reURLInformation = new RegExp([
// protocol
'^(https?:)//',
// host (hostname and port)
'(([^:/?#]*)(?::([0-9]+))?)',
// pathname
'(/[^?#]*)',
// search
'(\\?[^#]*|)',
// hash
'(#.*|)$'
].join(''));
var match = url.match(reURLInformation);
return match && {
full: url,
protocol: match[1],
host: match[2],
hostname: match[3],
port: match[4],
pathname: match[5],
search: match[6],
hash: match[7]
}
}
});
<div class="container">
<div class="info-container">
<p>URL:</p>
<input type="text" id="URL">
<input type="button" id="opentab-button" value="Go!">
</div>
<div class="langcode-container">
<input type="checkbox" value="ko-kr"> KOR<br>
<input type="checkbox" value="nl-NL"> NLD<br>
<input type="checkbox" value="lang-check"> PTB<br>
<input type="checkbox" value="lang-check"> PTG<br>
<input type="checkbox" value="lang-check"> CHT<br>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
答案 2 :(得分:-1)
var str = document.getElementById("URL").value
var res = str.replace(str.split("/")[3],"whateveryouwant")
window.open(res,'_blank');