使用jquery交换div的内容
我有一点html:使用jquery交换div的内容,jquery,html,Jquery,Html,我有一点html: <div id="storeBtn"> <a href="http://itunes.apple.com/artist" target="itunes_store" class="button">Download on iTunes</a> </div> 如果我检测到Android,我想将其更改为: <div id="storeBtn"> <a href="http://amazon.co
<div id="storeBtn">
<a href="http://itunes.apple.com/artist" target="itunes_store" class="button">Download on iTunes</a>
</div>
如果我检测到Android,我想将其更改为:
<div id="storeBtn">
<a href="http://amazon.com/artist" class="button">Download on Amazon</a>
</div>
这是我的尝试:
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
$("#storeBtn").html("<a href='http://amazon.com/artist' class='button'>Download on Amazon</a>");
}
var ua=navigator.userAgent.toLowerCase();
var isAndroid=ua.indexOf(“安卓”)>-1;/&&ua.indexOf(“移动”);
if(isAndroid){
$(“#storeBtn”).html(“”);
}
但一切都没有改变。我还尝试了replaceWith()而不是html()。此代码需要等待Dom就绪后才能执行:
$(function() {
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
$("#storeBtn").html("<a href='http://amazon.com/artist' class='button'>Download on Amazon</a>");
}
}
$(函数(){
var ua=navigator.userAgent.toLowerCase();
var isAndroid=ua.indexOf(“安卓”)>-1;/&&ua.indexOf(“移动”);
if(isAndroid){
$(“#storeBtn”).html(“”);
}
}
不要将代码放在标题部分,而是放在正文末尾的脚本元素中
...
<script>
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
$("#storeBtn").html("<a href='http://amazon.com/artist' class='button'>Download on Amazon</a>");
}
</script>
</body>
</html>
。。。
var ua=navigator.userAgent.toLowerCase();
var isAndroid=ua.indexOf(“安卓”)>-1;/&&ua.indexOf(“移动”);
if(isAndroid){
$(“#storeBtn”).html(“”);
}
当你的代码在头上时,它会在将div添加到DOM之前执行。你是否等待DOM准备就绪?你是否检查了isAndroid的值?你是否确定你使用的是android?如果你在没有用户代理转换器的pc上测试,那么没有骰子。你是否检查了isAndroid是否为真?是的,我可以如果isAndroid,我会显示一个警报。你的js控制台告诉你什么?是$defined吗?你尝试设置断点了吗?我尝试了这个,但仍然没有发生任何事情,事实上,如果我发出警报检查isAndroid,它不会触发。我应该可以把你的代码放在head部分,对吗?