0
如果有人從iOS調用頁面,我嘗試替換href
。問題在於JavaScript不會僅替換搜索到的文本,而是替換所有的初始鏈接。例如:如何僅通過替換一些文本來更改href標記
<a href="http://example.com" class='dynamicLink'>test</a>
當iOS手機或平板電腦在這個環節
<a href="http://ios+example.com" class='dynamicLink'>test</a>
但JavaScript的改變取代所有
<a href="http://ios+" class='dynamicLink'>test</a>
沒有人知道問題
window.onload = function changeLinks() {
var href = "http://ios+"
var links =
document.getElementsByClassName('dynamicLink');
if (navigator.userAgent.match(/Android/i) ||
navigator.userAgent.match(/webOS/i) ||
navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/iPod/i) ||
navigator.userAgent.match(/BlackBerry/i) ||
navigator.userAgent.match(/Windows Phone/i)
) {
for (var i = 0; i < links.length; i++) {
links[i].href = href.replace('http://', "http://ios+");
}
} else {
for (var i = 0; i < links.length; i++) {
links[i].href = href.replace('http://', "http://");
}
}
}
目前尚不清楚對我有什麼做你期待的代碼做...抱歉,但解釋相當混亂 – Alessio