简体   繁体   English

为什么我不能正确附加具有指定ID的标签? 是角色逃避问题吗?

[英]Why I can't correctly append a tag having the id specified? Is it a character escaping problems?

I am pretty new in JavaScript and Jquery and I have the following problem that I think could be related to some character escaping. 我在JavaScript和Jquery方面还很陌生,我遇到以下问题,我认为这可能与某些字符转义有关。

So I have this simple JQuery script: 所以我有这个简单的JQuery脚本:

<script>
    $( document ).ready(function() {
      //alert(exludeUserAgent());
      if(exludeUserAgent()) {
          //$( "#sezioneBenvenuto" ).remove();
          //$( "#sezioneLogo" ).remove();
          $( "#sezioneLogin" ).remove();

          $( ".container" ).append( "<section id="sezioneErrore"><p>TEST</p></section>" );
      }
    });
</script>

The problem is that when the append statment is performed it give me the following error: 问题在于,执行附加语句时,会出现以下错误:

**SCRIPT1006: Previsto ')'**

on this line: 在这条线上:

$( ".container" ).append( "<section id="sezioneErrore"><p>TEST</p>

If I remove the id="sezioneErrore" and I append a section tag without the id definition I have no problem. 如果我删除了id =“ sezioneErrore”并追加了没有id定义的section标签,那么我没有问题。

Why? 为什么? What am I missing? 我想念什么? The problem could be the " character of the id="sezioneErrore" ? If yes how can I try to escape it? 问题可能是“ id =“ sezioneErrore的字符”吗?如果是,我如何尝试转义它?

The string is breaking as you've used double quote that is causing the syntax error. 由于您使用了引起语法错误的双引号,因此字符串中断。

To solve this error, you can either 要解决此错误,您可以

  1. Use double quote inside single quote (and vice versa) 在单引号内使用双引号 (反之亦然)
  2. Escape the quotes inside the string 转义字符串中的引号

Examples: 例子:

Use single quotes outside 在外面使用单引号

$(".container").append('<section id="sezioneErrore"><p>TEST</p></section>');

Or single quotes inside double quotes 或双引号内的单引号

$(".container").append("<section id='sezioneErrore'><p>TEST</p></section>");

If you want to use double quotes inside double quote, escape it 如果要在双引号中使用双引号,请对其进行转义

$(".container").append("<section id=\"sezioneErrore\"><p>TEST</p></section>");

Same for single quotes 单引号相同

$(".container").append('<section id=\'sezioneErrore\'><p>TEST</p></section>');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM