【问题标题】:Jquery Uncaught Error Syntax error, unrecognized expression: li/labelJquery Uncaught Error 语法错误,无法识别的表达式:li/label
【发布时间】:2015-10-12 03:06:24
【问题描述】:

我想插入一个 cookie 通知栏。现在在旧时事通讯页面上,我收到如下错误:

未捕获的错误:语法错误,无法识别的表达式:li/label

时事通讯中的以下代码是

if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', cmxform, false );

    function cmxform(){
      // Hide forms
      $( 'form.cmxform' ).hide().end();

      // Processing
      $( 'form.cmxform' ).find( 'li/label' ).not( '.nocmx' ).each( function( i ){
      var labelContent = this.innerHTML;
      var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
      var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
      this.style.display = '-moz-inline-box';
      this.innerHTML = null;
      this.appendChild( labelSpan );
      } ).end();

      // Show forms
      $( 'form.cmxform' ).show().end();
    }

    //function to check empty fields

    function isEmpty(strfield1, strfield2) {

    strfield1 = document.forms.newsletter_subscribe.nome.value 
    strfield2 = document.forms.newsletter_subscribe.email.value

      if (strfield1 == "" || strfield1 == null || !isNaN(strfield1) || strfield1.charAt(0) == ' ')
      {
      alert("Please insert your name!")
      return false;
      }

      if (strfield2 == "" || strfield2 == null || strfield2.charAt(0) == ' ')
      {
      alert("Please insert a valid Email!")
      return false;
      }

      return true;
    }

    //function to check valid email address
    function isValidEmail(){
      validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
      strEmail = document.forms.newsletter_subscribe.email.value;

     // search email text for regular exp matches
      if (strEmail.search(validRegExp) == -1) 
     {
      alert('Email not valid! Please retry!');
      return false;
      } 
      return true; 
    }

    //function to check privacy
    function Privacy()
     {
     if (document.forms.newsletter_subscribe.checkbox.checked==false)
      {
      alert('Please accept the privacy conditions!');
      return false;
      }
      return true;
    }

    //function that performs all functions, defined in the onsubmit event handler

    function check(){
    if (isEmpty()){
            if (isValidEmail()){
                if (Privacy()) {
              return true;
            }
        }
    }
        return false;
    }

    //**********************************************************************************************
    //function to check empty fields unsubscribe form

    function isEmptyEmail(strfield1) {

    strfield1 = document.forms.newsletter_unsubscribe.email.value

      if (strfield1 == "" || strfield1 == null || !isNaN(strfield1) || strfield1.charAt(0) == ' ')
      {
      alert("Please insert a valid Email!")
      return false;
      }


      return true;
    }

    //function to check valid email address
    function isValidEmailCancel(){
      validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
      strEmail = document.forms.newsletter_unsubscribe.email.value;

     // search email text for regular exp matches
      if (strEmail.search(validRegExp) == -1) 
     {
      alert('Email not valid! Please retry!');
      return false;
      } 
      return true; 
    }

    //function that performs all functions, defined in the onsubmit event handler

    function check_unsubscribe(){
    if (isEmptyEmail()){
            if (isValidEmailCancel()){
              return true;
        }
    }
        return false;
    }

删除新资源时,该通讯脚本就可以了。但是,当使用新的 jQuery 资源时会出现此错误。

【问题讨论】:

  • 错误告诉我一切:$('form.cmxform').find('li/label')
  • 是的,但是当我使用最新的旧 jQuery.js(从 2006 年开始)时,时事通讯脚本就可以了...

标签: javascript jquery newsletter uncaught-exception


【解决方案1】:

如果你想在 jQuery 中连接多个选择器,发现你使用了错误的语法

$( 'form.cmxform' ).find( 'li/label' ).not( '.nocmx' ).each( function( i ){

应该变成:

$( 'form.cmxform' ).find( 'li, label' ).not( '.nocmx' ).each( function( i ){

这意味着您正在您选择的 DOM 范围内寻找任何 li OR label 标签('form.cmxform')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多