2012-05-30 70 views
0

我試圖設置jQuery手機中錨點標記的點擊處理程序,通過引用錨點標記內的處理函數(我需要這樣做是出於某種原因這很麻煩)。如下面的代碼所示,當我將一個變量傳遞給處理函數時,它是一個整數,但是當它傳遞一個字符串時,它不會。任何人都可以解釋這是乳清嗎?jQuery Mobile Clickhandler適用於整數輸入,但不適用於字符串

<head> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.css" 
    /> 
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> 
    <script src="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.js"></script> 
</head> 

<body> 
    <script> 
     $(document).ready(function() { 

      //The Line Below works 
      $("#main").html('<a href="#" data-theme="a" onclick="test(' + '2' + '); return false" rel="external" data-role="button">click test</a>'); 

      /* 
//The Lines Below Do not work 
//$("#main").html('<a href="#" data-theme="a" onclick="test('+'hello world'+'); return false" rel="external" data-role="button">click test</a>'); 
var test = 'hello world'; 
//$("#main").html('<a href="#" data-theme="a" onclick="test('+test+'); return false" rel="external" data-role="button">click test</a>'); 
*/ 

      $("#main").trigger('create') 

     }); 

     function test(e) { 
      alert(e); 
     } 
    </script> 
    <div data-role="page" class="type-interior"> 
     <div id="main" data-role="content"></div> 
    </div> 
</body> 

回答

0

嘗試這種::

var testParam = "'hello world'"; 

$("#main").html('<a href="#" data-theme="a" onclick="test('+testParam +'); return false;" rel="external" data-role="button">click test</a>'); 
相關問題