zoukankan      html  css  js  c++  java
  • #wordpress 中JQuery使用问题 报错:Uncaught TypeError: $ is not a function【wordpress问题记录】

    在企图通过css隐藏掉wordpress个人资料中的first-name 和 last-name时候,
    在这里找到了相关的参考代码:
    https://noexceptions.io/disabling-first-and-last-name-changes-in-the-wordpress-profile/

    // Function to disable the first name and last name fields
    function disable_first_and_last_name_fields() {
    	?>
    	<script type="text/javascript">
            $(function() {
                // Disable the first and last names in the admin profile so that user's cannot edit these
    				$('#first_name').prop( 'disabled', true );
    				$('#last_name').prop( 'disabled', true );
            });
       	</script>
    	<?php
    }
    
    // Action hook to inject the generated JavaScript into admin pages
    add_action( 'admin_head', 'disable_first_and_last_name_fields' );
    

    比较简单,就是通过wordpress提供的action钩子,在内容管理页面注入jQuery脚本,把相关dom节点移除。

    但是,直接在function.php中贴入的时候,发现并不起作用。

    通过alert和console调试,发现代码块并未执行。

    // Function to disable the first name and last name fields
    function disable_first_and_last_name_fields() {
    	?>
    	<script type="text/javascript">
            $(function() {
    			console.log("smt to be console");
    			alert("alert smt");
                // Disable the first and last names in the admin profile so that user's cannot edit these
    			$('#first_name').prop( 'display', false );
    			$('#last_name').prop( 'disabled', true );
            });
       	</script>
    	<?php
    }
    
    // Action hook to inject the generated JavaScript into admin pages
    add_action( 'admin_head', 'disable_first_and_last_name_fields' );
                
    

    最后在stackoverflow上找到了该问题的原因:

    https://stackoverflow.com/a/45346975/12261182

    就是把代码段中的$换作jQuery即可。

    var $ = jQuery;
    
    
    // Function to disable the first name and last name fields
    function disable_first_and_last_name_fields() {
    	?>
    	<script type="text/javascript">
    		let $ = jQuery;
            $(function() {
    			console.log("smt to be console");
    			alert("alert smt");
                // Disable the first and last names in the admin profile so that user's cannot edit these
    			$('#first_name').prop( 'display', false );
    			$('#last_name').prop( 'disabled', true );
            });
       	</script>
    	<?php
    }
    
    // Action hook to inject the generated JavaScript into admin pages
    add_action( 'admin_head', 'disable_first_and_last_name_fields' );
                
    

    注意,这里,let $ = jQuery不会对其它的页面造成影响。但是在"Add New User"页面的时候,运行会出现"$ has been declared"的报错,为了保起见,应该不使用$符号,你可以指定任意其它的符号变量,例如let $$ = jQuery. 或者为了后期更加便于维护,请直接使用jQuery较为稳妥。

    It works !

    这个问题在这里有更加详细的讨论:https://stackoverflow.com/questions/12258282/typeerror-is-not-a-function-wordpress

    https://stackoverflow.com/a/12258427/12261182

  • 相关阅读:
    Mysql 多字段去重
    一个不错的PHP二维数组排序函数简单易用存用
    关于威富通的微信扫码支付处理思路和流程
    牛顿迭代法求解平方根
    impex 语法
    屏幕取词技术实现原理与关键源码
    支持向量机通俗导论(理解SVM的三层境界)
    用 Chrome 扩展实现修改
    可编辑表格
    在浏览器右键添加自定义菜单
  • 原文地址:https://www.cnblogs.com/jaycethanks/p/13234763.html
Copyright © 2011-2022 走看看