【问题标题】:**ERROR:** "Uncaught TypeError: Cannot read property 'replace' of undefined"?**错误:**“未捕获的类型错误:无法读取未定义的属性‘替换’”?
【发布时间】:2018-03-20 18:05:47
【问题描述】:

错误:“未捕获的类型错误:无法读取未定义的属性‘替换’”?

jQuery(document).ready(function(){
    	var rep = jQuery(".abc")
              .clone()
              .wrap("<div></div>")
              .parent().html()
              .replace(/select/g,"ul")
              .replace(/option/g,"li");

    	jQuery(".abc").replaceWith(rep);
    });
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

【问题讨论】:

    标签: jquery


    【解决方案1】:

    我认为这按预期工作:

    jQuery(document).ready(function(){
    	var rep = jQuery(".abc")
              .clone()
              .wrap("<div></div>")
              .parent().html()
              .replace(/select/g,"ul")
              .replace(/option/g,"li");
    
    	jQuery(".abc").replaceWith(rep);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="abc">
      <select>
        <option>1</option>
        <option>2</option>
      </select>
    </div>

    您的页面上可能缺少.abc 元素。在这种情况下,.html() 将返回 undefined

    【讨论】:

      猜你喜欢
      • 2014-06-09
      • 2021-11-03
      • 1970-01-01
      • 2017-02-11
      • 1970-01-01
      • 2014-06-14
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      相关资源
      最近更新 更多