3

我在我的项目中使用 Web 浏览器控件,我可以使用此控件轻松显示 html 数据,
现在我正在尝试在此 html 中包含 jquery,但它似乎不起作用

  WebBrowser webwsr = new WebBrowser();
  String WebBrwseHTML = "<html><head><script type='text/javascript' src='jquery-1.7.1.js'></script><script type='text/javascript'>$(document).ready(function () {  $('div').css('background-color', 'Red'); })</script></head><body><div>DUMMY</div></body></html>";

   webwsr.NavigateToString(WebBrwseHTML);

我在这里做错了什么

4

2 回答 2

1

不是 jq 专家,但试试这个:

 StringBuilder sb = new StringBuilder();
        sb.AppendLine("<html>");
        sb.AppendLine("<head>");
        sb.AppendLine(" <script src='http://code.jquery.com/jquery-latest.js'></script>");
        sb.AppendLine("<script>");
        sb.AppendLine("$(document).ready(function () {");
        sb.AppendLine("$('div').css('background-color', 'Red'); });");
        sb.AppendLine("</script>");
        sb.AppendLine("</head>");
        sb.AppendLine("<body>");
        sb.AppendLine("<div>DUMMY</div>");
        sb.AppendLine("</body>");
        sb.AppendLine("</html>");

        WebBrowser webwsr = new WebBrowser();
        String WebBrwseHTML = sb.ToString();
        webwsr.NavigateToString(WebBrwseHTML);
        mainGrid.Children.Add(webwsr);
于 2012-08-27T08:00:56.180 回答
1

我的建议是使用System.IO.File.ReadAllText(jqueryFilePath)来阅读基本的 jQuery 代码,而不是<script src="..."></script>使用<script>" + jquery + "</script>.

下面是一个工作示例:(@"C:\jquery.txt"用您自己的路径替换)

var jquery = File.ReadAllText(@"C:\jquery.txt");

var html = @"<html>
<head>
    <script type='text/javascript'>"+jquery+@"</script>
    <script type='text/javascript'>
        $(document).ready(function() {
            $('div').css('background-color', 'Red');
        })
    </script>
</head>

<body>
    <div>DUMMY</div>
</body>

</html>";

browser.NavigateToString(html);
于 2018-07-06T13:42:59.380 回答