javascript - 为什么我从 here-string 中收到 "not a cmdlet"错误?

标签 javascript jquery html powershell

下面是我的 html 代码的摘录,在我的 Powershell 脚本中定义为 $html:

$html = @"
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <script type="text/javascript" src="/js/lib/dummy.js"></script>

  <link rel="stylesheet" type="text/css" href="/css/result-light.css">
  <link rel="stylesheet" type="text/css" href="https://rawgit.com/pguso/jquery-plugin-circliful/1.0.2/css/jquery.circliful.css">
  <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script type="text/javascript" src="https://rawgit.com/pguso/jquery-plugin-circliful/1.0.2/js/jquery.circliful.js"></script>
    <style type="text/css"></style>

    <title>Orders</title>

    </head>
        <body>
            <div class="container">
                <h3 style="text-align: center; margin-top: 2%; font-size: 300%">Actual Vs Expected orders for Today:</h3>
                <h2 style="text-align: center; color: #ff3333; margin-top: 2.5%; font-size: 500%">$SAPTodayExel<small style="color: #000000">/$ExpectedExelOrders</small></h2>                              
                <div class="col-lg-12">
                    <div id="test-circle"></div>
                    <table style="margin-top: 100px; width:75%; position: fixed; bottom: 40px;">

 <tr>
    <th style="font-size: 16px; width:11%; background: linear-gradient(to bottom,  #f0f9ff 0%,#cbebff 49%,#a1dbff 100%)">12</th>
    <td style="font-size: 30px; color: #ff3333; width:20%; background: linear-gradient(to bottom, #f0f9ff 0%,#cbebff 49%,#a1dbff 100%)">$SAPTodayNonConExel</td>
    <th style="font-size: 16px; width:8%; background: linear-gradient(to bottom,  #f0f9ff 0%,#cbebff 49%,#a1dbff 100%)">13</th>
    <td style="font-size: 30px; color: #ff3333; width:20%; background: linear-gradient(to bottom, #f0f9ff 0%,#cbebff 49%,#a1dbff 100%)">$SAPTodayPOSExel</td>
    <th style="font-size: 16px; width:8%; background: linear-gradient(to bottom,  #f0f9ff 0%,#cbebff 49%,#a1dbff 100%)">24</th>
    <td style="font-size: 30px; color: #ff3333; width:15%; background: linear-gradient(to bottom, #f0f9ff 0%,#cbebff 49%,#a1dbff 100%)">$SAPTodayROIExel</td>
    <th style="font-size: 16px; width:7%; background: linear-gradient(to bottom,  #f0f9ff 0%,#cbebff 49%,#a1dbff 100%)">245</th>
    <td style="font-size: 30px; color: #ff3333; width:20%; background: linear-gradient(to bottom, #f0f9ff 0%,#cbebff 49%,#a1dbff 100%)">$SAPTodayUnited</td>    
  </tr>
</table>
                </div>
            </div>
    </head>

<script type='text/javascript'>
        window.onload=function(){
        $( document ).ready(function() { // 6,32 5,38 2,34
            $("#test-circle").circliful({
            foregroundColor: "#cb60b3",
            backgroundColor: "#e5e7e9",
            pointColor: "none",
            fillColor: 'none',
            foregroundBorderWidth: 15,
            backgroundBorderWidth: 15,
            pointSize: 28.5,
            fontColor: '#aaa',

            animation: 1,
            animationStep: 5,

            showPercent: 1,
            noPercentageSign: false,
            replacePercentageByText: null,
            percentageTextSize: 22,         
            percent: $PercentReceived,
            multiPercentage: 0,
            percentages: null,

            targetPercent: null,
            targetTextSize: 12,
            targetColor: '#2980B9',

            icon: 'none',
            iconSize: '30',
            iconColor: '#ccc',
            iconPosition: 'top',

            target: 0,
            start: 0,

            textBelow: true,            
            text: null,
            textStyle: "font-size: 10px",
            textColor: '#17202a',
            textAdditionalCss: 'test',

            halfCircle: false,
            animateInView: false,
            decimals: 0,
            alwaysDecimals: false
            });
        });
    }
        </script>
</html>
"@

$html | out-file "c:\test.html"

当我将其作为 html 文件运行时,它运行完全正常并且脚本按预期运行。但是,如果我通过 PS 解析它会出现错误:

document : The term 'document' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:46 char:6 + $( document ).ready(function() { // 6,32 5,38 2,34 + ~~~~~~~~ + CategoryInfo : ObjectNotFound: (document:String) [], CommandNot FoundException + FullyQualifiedErrorId : CommandNotFoundException

我尝试将“$( document )”更改为“$(”document”),添加引号,这似乎有效,但是当您查看输出的 html 文件时,它会删除“$(”和")",因此它有效地禁用了由于语法不正确而启用的脚本。

是因为它将美元符号视为 PS 变量吗?我该如何解决这个问题?

谢谢

最佳答案

如您所知,问题行有这个

$( document )

这是 PowerShell 的语法 subexpressions $() . PowerShell 告诉您“文档”不是命令。在 PowerShell 中使用反引号转义“$”,这样它就不会被特殊对待

`$( document )

您对以下行有类似的问题,但可能不那么明显

$("#test-circle").circliful({

处理后在您的此处字符串中看起来像这样

#test-circle.circliful({

您还需要转义那个美元符号。


您可以只使用带单引号的 herestring (@' '@),这样就可以避免这个问题。但是,由于您是变量替换,因此您不能在没有更多更改的情况下走这条路。

关于javascript - 为什么我从 here-string 中收到 "not a cmdlet"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40809697/

相关文章:

javascript - 使用 React 单击时输入的值更新 URL

javascript - mysql datetime 数据到 html5 datetime-local 字段使用 javascript 和 PHP

python - 将 Python 列表导入 HTML 表的最佳方法?

javascript - 如何将 jQuery Accordion 默认设置为未打开?并切换打开/关闭?

java - 无法通过 Android/Java 提取 HTML 数据

javascript - jQuery 在 remove() 之后取消选中复选框;

html - Wordpress 菜单没有正确显示?

javascript - 更新 MongoDB 数组中的多个值

javascript - 如何使用 Scrapy 和 Splash 抓取基于 AJAX 的网站?

jquery .load() 在 IE/Safari 中不起作用