我有一個新的 Android Xamarin 解決方案。我想在 WebView 中嵌入一個執行 js 腳本的 HTML 頁面。我在 Android 資產中添加了這個 html 和這個 js 頁面。
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App1.MainPage">
<StackLayout>
<Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
<Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
</Frame>
<Label Text="Start developing now" FontSize="Title" Padding="30,10,30,10"/>
<Label Text="Make changes to your XAML file and save to see your UI update in the running app with XAML Hot Reload. Give it a try!" FontSize="16" Padding="30,0,30,0"/>
<Label FontSize="16" Padding="30,24,30,0">
<Label.FormattedText>
<FormattedString>
<FormattedString.Spans>
<Span Text="Learn more at "/>
<Span Text="https://aka.ms/xamarin-quickstart" FontAttributes="Bold"/>
</FormattedString.Spans>
</FormattedString>
</Label.FormattedText>
</Label>
<WebView x:Name="webView" WidthRequest="1000" HeightRequest="1000" />
</StackLayout>
</ContentPage>
MainPage.xaml.cs
using Xamarin.Forms;
namespace App1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
webView.Source = "file:///android_asset/titi.html";
}
}
}
titi.html
<html>
<body>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<h1>Webview Test</h1>
<br />
Enter name: <input type="text" id="name">
<br />
<br />
<button type="button" onclick="javascript: displayName($('#name').val());">Test Button</button>
<br />
<p id="result">Result:</p>
<script src="./titi.js"></script>
</body>
</html>
titi.js
function displayName(str) {
try {
$('#result').text($('#result').text() " " str);
}
catch (err) {
log(err);
}
}
HTML頁面正確顯示,但當我單擊按鈕時未執行js部分。
你能幫我解除封鎖嗎?
uj5u.com熱心網友回復:
如果我不在 js 檔案中使用 jquery,它會起作用。
function displayName(str) {
var result = document.querySelector('#result');
result.innerHTML = str;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/507019.html
標籤:javascript 安卓 html xamarin xamarin.android