2013-05-10 48 views
0

嗨,大家好,我似乎無法找到我想要做的確切的事情,並且我是一個javascript愛好者,我似乎無法對它進行爭吵。 基本上... 我有此腳本連接到各種元素在我的網頁用javascript/jQuery切換桌面到手機的href url

$(document).ready(function() { 
$("h1, h2, h5, .class1, .class2 #image1").click(function() { 
    window.open('https://www.linkdesktop.com'); 
}); 

});

什麼,我想要做的是: 如果移動設備再切換www.linkdesktop.com TO www.linkmobile.com

這是可能的,我做的根據屏幕大小或使用某種移動偵測腳本?

解決讚賞,非常感謝。


好了,所以謝謝你的答案

所以也許財產以後也是這樣嗎?

var userAgent = window.navigator.userAgent; 

$(document).ready(function() { 
if((Android|webOS|iPhone|iPad|iPod|BlackBerry).test(navigator.userAgent)) { 

$("h1, h2, h5, .class1, .class2 #image1").click(function() { 
    window.open('https://www.linkmobile.com'); 
}); 

} 
else { 

$("h1, h2, h5, .class1, .class2 #image1").click(function() { 
    window.open('https://www.linkdesktop.com'); 
}); 

} 

}); 

回答

1

在我的最後一個項目,我用這個solution to check mobile user。優雅而簡單。

var isMobile = { 
    Android: function() { 
    return navigator.userAgent.match(/Android/i); 
    }, 
    BlackBerry: function() { 
    return navigator.userAgent.match(/BlackBerry/i); 
    }, 
    iOS: function() { 
    return navigator.userAgent.match(/iPhone|iPad|iPod/i); 
    }, 
    Opera: function() { 
    return navigator.userAgent.match(/Opera Mini/i); 
    }, 
    Windows: function() { 
    return navigator.userAgent.match(/IEMobile/i); 
    }, 
    any: function() { 
    return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); 
    } 
}; 

if(isMobile.any()) alert('Mobile'); 
+0

好的,謝謝所以如果我使用你的方法與上面聲明的變量這是應該工作: '$(document).ready(function(){ if(isMobile.any()){ $ {「h1,h2, 。H5,.class1,.class2#圖像1"​​ )點擊(函數(){ window.open( 'https://www.linkmobile.com'); }); } 其他{ \t $ (「h1,h2,h5,.class1,.class2#image1」)。click(function(){window.open('https://www.linkdesktop.com'); }); } }); ' – James 2013-05-10 13:43:09

+0

是的。但是,爲了全局聲明isMobile變量,我要把isMobile inizialization和聲明放在$(document).ready(function(){var isMobile = {...};)中。 < - 如此。 – 2013-05-10 13:50:27

+0

感謝它的工作! – James 2013-05-10 14:06:57

相關問題