我正在编写允许用户在该域上创建自定义的代码。正如您在下面的示例中看到的那样,用户可以选择example.com,并通过从下面的select标记中进行选择,将其更改为任何 example(n).com我试图实现的是,当我选择example1.com或example3.comttp://将变为https:// 示例: 当我从选择列表中选择example1.com时,输出将是 https://example1.com https://example3.com 但如果我选择example2.com/example4.com,它将保持原样。 http://example2.com http://example4.com 我正试图在jquery中实现它。如有任何帮助和建议,我们将不胜感激。function changeText(url)
{
document.getElementById('mydiv').innerHTML = url;
}
<div>http://<span id='mydiv'>example0.com</span></div>
<select onchange='changeText(this.value)'>
<option>example1.com</option>
<option>example2.com</option>
<option>example3.com</option>
<option>example4.com</option>
</select>
查看此片段。
function changeText(urlPostFixNumber) {
var protocol;
var url;
var domain = '://example' + urlPostFixNumber + '.com';
switch(urlPostFixNumber) {
case "1":
case "3":
protocol = 'https';
break;
default:
protocol = 'http';
}
url = protocol + domain;
document.getElementById('mydiv').textContent = url;
}
<p id="mydiv">http://example0.com</p>
<select onchange="changeText(this.value)">
<option value="0" selected>example0.com</option>
<option value="1">example1.com</option>
<option value="2">example2.com</option>
<option value="3">example3.com</option>
<option value="4">example4.com</option>
</select>