2013-04-08 91 views
1

我需要用另一個標籤替換某個類的html「a」標籤。jquery mobile用另一個超鏈接替換超鏈接

這是我的代碼

<html> 
<head> 

<script type="text/javascript"src="jquery.mobile-1.3.0"></script> 
<link rel="stylesheet" href="jquery.mobile-1.3.0" type="text/css"> 

<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript"> 

$('.list1').replaceWith('<a class="list3" href="#">link3</a>'); 
</script> 

</head> 

<body> 

<h1> hello world </h1> 

<div class="content-page" > 
    <div class="list-page"> 
    <a class="list1" href="#">link1</a> 
    <a class="list2" href="#">link2</a> 
    </div> 
</div> 
</body> 

</html> 

我用了jQuery replaceWith功能,但令我失望的是沒有在這種情況下,爲我工作。任何幫助是極大的讚賞。謝謝。

回答

1

解決方案:

<html> 
<head> 
    <!-- STYLESHEETS //--> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> 
    <!-- JavaScripts //--> 
    <script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js"></script> 
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
     $('.list1').replaceWith('<a class="list3" href="#">link3</a>'); 
    }); 
    </script> 
</head> 
<body> 
<h1> hello world </h1> 
<div class="content-page" > 
    <div class="list-page"> 
    <a class="list1" href="#">link1</a> 
    <a class="list2" href="#">link2</a> 
    </div> 
</div> 
</body> 

你需要用你的代碼文件準備好。

或者,如果你想jQuery Mobile的版本:

<html> 
<head> 
    <!-- STYLESHEETS //--> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> 
    <!-- JavaScripts //--> 
    <script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js"></script> 
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script> 
    <script type="text/javascript"> 
    $(document).on('pagebeforeshow', '.content-page', function() { 
     $('.list1').replaceWith('<a class="list3" href="#">link3</a>'); 
    }); 
    </script> 
</head> 
<body> 
<h1> hello world </h1> 
<div class="content-page" data-role="page"> 
    <div class="list-page"> 
    <a class="list1" href="#">link1</a> 
    <a class="list2" href="#">link2</a> 
    </div> 
</div> 
</body> 

編輯:

在你的榜樣,更換這些線路:

<script type="text/javascript"src="jquery.mobile-1.3.0"></script> 
<link rel="stylesheet" href="jquery.mobile-1.3.0" type="text/css"> 
<script type="text/javascript" src="jquery.js"></script> 

有了這個線:

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> 
<!-- JavaScripts //--> 
<script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js"></script> 
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"</script> 

基本上先初始化jQuery Mobile css,然後初始化jQuery然後初始化jQuery Mobile。在你的例子中,你正在jQuery Mobile之前初始化jQuery Mobile,可能會導致錯誤。

+0

當我用你的jquery語句它沒有工作。但是當我複製你的整個代碼它的工作。我在這裏錯過了什麼? :( – crowso 2013-04-08 16:24:11

+0

你是在說我的第一個還是第二個例子? – Gajotres 2013-04-08 16:45:44

+0

當我用你的代替我的jquery代碼行時,它不起作用,請幫助我。 – crowso 2013-04-08 16:47:17

0

另外,在DOM操作之後使用以下可能是優選的。這是爲了將JQuery手機CSS應用回元素。

$(":jqmData(role='page')").trigger('create'); 

或特別

$(".list-page").trigger('create'); 
如果要刷新特定部分的CSS DOM中的