2013-01-19 48 views
2

我正在使用Visual Studio 2012創建示例Windows 8 Phone應用程序。Jquery不適用於在Visual Studio 2012中使用HTML 5模板創建的Windows 8 Phone應用程序

從我選擇了 「創建新項目」 選項,

的Windows> Windows Phone的HTML5應用

我還添加了一個jquery.min.js文件,以項目爲示下面。

enter image description here

下面是我編寫的代碼中的index.html ...

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
     <link rel="stylesheet" type="text/css" href="/html/css/phone.css" /> 
     <script type="text/javascript" src="/html/js/jquery.min.js" ></script> 
     <title>Windows Phone</title> 
     <script type="text/javascript"> 
      $("#dynamic-box").text("hey !"); 
     </script> 
    </head> 
    <body> 
     <div> 
      <p>MY APPLICATION</p> 
     </div> 
     <div id="dynamic-box"></div> 
    </body> 
</html> 

但無論我怎麼努力我的jQuery代碼只是不會工作。有沒有其他的方式在jQuery中寫入Windows 8 Phone應用程序?

請幫我解決這個問題。

回答

2

兩件事情會在這裏,一個jQuery的,一個Windows Phone的:

  1. 把你的代碼放到ready事件(它不會在網頁中工作,如爲)

    <script type="text/javascript"> 
        $(document).ready(function() { 
         $("#dynamic-box").text("hey!"); 
        }); 
    </script> 
    
  2. 設置IsScriptEnabled導航到開始頁面之前。翻轉Browser_LoadedMainPage.xaml.cs的報表順序。

    private void Browser_Loaded(object sender, RoutedEventArgs e) 
    { 
        // Add your URL here 
        Browser.IsScriptEnabled = true; 
        Browser.Navigate(new Uri(MainUri, UriKind.Relative)); 
    } 
    
+0

謝啦。 。 有效 ! – Yasser

+0

另外,您需要使用MS批准的jQuery版本。 您可以使用NuGet包管理器(Project> Manage NuGet Packeges)將其添加到您的項目中。 – codingPear

1

我最近W¯¯rote a post這一點,以爲在這裏分享它,它是有用的......

第1步:添加jquery.min.js到解決方案

enter image description here

步驟2:更改在Browser_Loaded方法語句的順序如下所示

private void Browser_Loaded(object sender, RoutedEventArgs e) 
{ 
    // Add your URL here 
    Browser.IsScriptEnabled = true; 
    Browser.Navigate(new Uri(MainUri, UriKind.Relative)); 
} 

步驟3:樣品jQuery代碼

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
     <link rel="stylesheet" type="text/css" href="/html/css/phone.css" /> 
     <script type="text/javascript" src="/html/js/jquery.min.js" ></script> 
     <title>Windows Phone</title> 
     <script type="text/javascript"> 
     $(document).ready(function() { 
      $("#message").text("Welcome !"); 

      $("#goBtn").click(function(){ 
      $("#message").text("button has been pressed"); 
      }); 
     }); 
     </script> 
    </head> 
    <body> 
     <div> 
      <p>MY APPLICATION</p> 
     </div> 
     <div id="message"></div> 
     <input type="button" id="goBtn" value="press me !" /> 
    </body> 
</html> 

輸出

enter image description here

+0

我喜歡全功能的答案。你能爲phonegap/Cordova v3創建一個編輯過的更新嗎? –

相關問題