星期三 , 22 1 月 2025

Jquery ajax时候验证当前dom是否为空

const loadPostsButton = $("#wp-learn-ajax-button"); 

	if (loadPostsButton != 'undefined' && loadPostsButton != null) {

		loadPostsButton.on('click',function(event){
			$.post(wp_learn_ajax.ajax_url,{action:'learn_fetch_posts'},function(posts) {
				const textarea = $("#load_posts");

				posts.forEach(function(post) {
					textarea.append(post.post_title + '\n');
				});
			});

		});