首页 > Web开发 > 详细

#wordpress 中JQuery使用问题 报错:Uncaught TypeError: $ is not a function【wordpress问题记录】

时间:2020-07-04 14:44:27      阅读:48      评论:0      收藏:0      [点我收藏+]

在企图通过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‘ );
            

It works !

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

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

#wordpress 中JQuery使用问题 报错:Uncaught TypeError: $ is not a function【wordpress问题记录】

原文:https://www.cnblogs.com/jaycethanks/p/13234763.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!