【问题标题】:javascript IF statement errors [duplicate]javascript IF语句错误[重复]
【发布时间】:2015-03-01 00:11:47
【问题描述】:

我遇到了一些 javascript 代码的问题,我找不到解决方案,希望你们中的一些人能帮助我找到解决方案。

问题似乎出在下面代码中的 if 语句if(a>b) 上。我得到不同的错误取决于我如何编写 if 语句结束取决于我在哪里看到不同的错误。

Chrome 的开发者工具说:

Uncaught SyntaxError: Unexpected token ;

IE 的开发者工具说:

Uncaught SyntaxError: Unexpected token )

如果我写 if(3>1),我会得到相同的结果,所以我认为问题不是 a 或 b。

如果我尝试if(b<a),那么 glassfish 会抛出一个异常,指出下面的错误,其中第 39 行是 if 语句

Error Traced[line: 39] 元素的内容必须由 格式正确的字符数据或标记。

if(1<3) 产生与if(b<a) 相同的结果

现在,如果我写 if(3===1) 或 if(3===3) 它工作正常,代码运行完美并产生使用“===”符号的预期结果,但现在我需要能够使用 < 或 @ 987654329@.

在将 glassfish 升级到 4.1 并将许多软件包升级到最新版本之前,此代码运行良好。

我已经重新编译了很多次并重新启动了服务器,但到目前为止没有成功。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:cc="http://java.sun.com/jsf/composite" xmlns:tp="http://java.sun.com/jsf/composite/components" xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<cc:interface>
  <cc:attribute name="text" type="java.lang.String" required="true" />
  <cc:attribute name="maxsize" type="java.lang.Integer" required="true" />
  <cc:attribute name="style" type="java.lang.String" required="false" />
</cc:interface>
<cc:implementation>

  <p id="par#{component.clientId}" style="#{cc.attrs.style}">
    <span id="span#{component.clientId}"><h:outputText value="#{cc.attrs.text}" /></span>
  </p>



  <script>
    var span = $("[id='span#{component.clientId}']");

    var linkShow = $("<a/>");
    linkShow.attr("id", "linkShow#{component.clientId}");
    linkShow.attr("style", "color: #07A0DD;");
    linkShow.text("Visa mer");
    linkShow.hide();

    var linkHide = $("<a/>");
    linkHide.attr("id", "linkHide#{component.clientId}");
    linkHide.attr("style", "color: #07A0DD;");
    linkHide.text("Visa mindre");
    linkHide.hide();

    var a = "#{cc.attrs.text}".length;
    var b = #{cc.attrs.maxsize};
    if (a > b) {
      span.text("#{cc.attrs.text}".substring(0, # {
        cc.attrs.maxsize
      }) + "... ");
      $("[id='par#{component.clientId}']").append(span);
      $("[id='par#{component.clientId}']").append(linkShow);
      $("[id='par#{component.clientId}']").append(linkHide);


      $("[id='linkShow#{component.clientId}']").show();
      $("[id='linkShow#{component.clientId}']").click(function() {
        $("[id='span#{component.clientId}']").text("#{cc.attrs.text}");
        $("[id='linkHide#{component.clientId}']").show();
        $("[id='linkShow#{component.clientId}']").hide();
      });

      $("[id='linkHide#{component.clientId}']").click(function() {
        $("[id='span#{component.clientId}']").text("#{cc.attrs.text}".substring(0, # { cc.attrs.maxsize}) + "... ");
        $("[id='linkShow#{component.clientId}']").show();
        $("[id='linkHide#{component.clientId}']").hide();
      });
    }
  </script>
</cc:implementation>

</html>

【问题讨论】:

  • var b = # { ???请打开你的控制台并调试它。不要仅仅因为语法错误而问关于 SO 的问题。 编辑:好的,我看到这与模板引擎有关,所以至少使用相关标签
  • 调试器通常也会给你导致错误的行,检查那行。
  • 我检查了调试,a 和 b 都给出了正确的值,在这种情况下,我测试了 a 给出 1006,b 是 200。正如上面的评论中提到的,我用 3 和 1 替换了 a 和 b它仍然产生相同的结果。
  • ; 是标准的 JavaScript。我猜var b = # { 只是复制粘贴错误?
  • 如果没有&lt; 或&gt; 也能正常工作,那么您的模板引擎中的问题是不是没有在xml 中管理这些符号?您应该将输出粘贴到 html 中,而不是 xml 中的模板。

标签: javascript jsf-2.2


【解决方案1】:

解决了这个问题。正如所评论的那样,问题出在模板引擎上,它无法理解某些运算符。通过将其放入 cdata 文件中,我解决了问题。

解决方案:

<script type="text/javascript">
    //<![CDATA[

        var span = $("[id='span#{component.clientId}']");

        var linkShow = $("<a/>");
        linkShow.attr("id", "linkShow#{component.clientId}");
        linkShow.attr("style", "color: #07A0DD;");
        linkShow.text("Visa mer");
        linkShow.hide();

        var linkHide = $("<a/>");
        linkHide.attr("id", "linkHide#{component.clientId}");
        linkHide.attr("style", "color: #07A0DD;");
        linkHide.text("Visa mindre");
        linkHide.hide();

        var a = "#{cc.attrs.text}".length;
        var b = #{cc.attrs.maxsize};
        if (a > b)
        {
            span.text("#{cc.attrs.text}".substring(0,#{cc.attrs.maxsize}) + "... ");
            $("[id='par#{component.clientId}']").append(span);
            $("[id='par#{component.clientId}']").append(linkShow);
            $("[id='par#{component.clientId}']").append(linkHide);


            $("[id='linkShow#{component.clientId}']").show();
            $("[id='linkShow#{component.clientId}']").click(function() {
                $("[id='span#{component.clientId}']").text("#{cc.attrs.text}");
                $("[id='linkHide#{component.clientId}']").show();
                $("[id='linkShow#{component.clientId}']").hide();
            });

            $("[id='linkHide#{component.clientId}']").click(function() {
                $("[id='span#{component.clientId}']").text("#{cc.attrs.text}".substring(0,#{cc.attrs.maxsize}) + "... ");
                $("[id='linkShow#{component.clientId}']").show();
                $("[id='linkHide#{component.clientId}']").hide();
            });
        }
       // ]]>
    </script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 2012-09-17
    • 2013-08-08
    • 2017-11-21
    • 1970-01-01
    • 2016-11-21
    相关资源
    最近更新 更多