2012-05-30 69 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>'); 
相关问题