我想要在tinyMCE編輯器(WordPress)裏面得到一個數量的評論標籤<!jQuery在tinyMCE編輯器中得到N個元素的數量
我在做什麼錯?
jQuery(document).ready(function($)
{
//find the count of nextpage elements
var n = jQuery("#content").find("<!--nextpage-->").length;
alert(n);
});
我想要在tinyMCE編輯器(WordPress)裏面得到一個數量的評論標籤<!jQuery在tinyMCE編輯器中得到N個元素的數量
我在做什麼錯?
jQuery(document).ready(function($)
{
//find the count of nextpage elements
var n = jQuery("#content").find("<!--nextpage-->").length;
alert(n);
});
評論是不可穿越的DOM節點,如輸入,divs等,jQuery不會找到它們。
相反,您可以嘗試對元素的innerHTML
內的特定註釋進行字符串搜索。
var count = $('#foo').html().split("<!--nextpage-->").length - 1;
觀看演示:http://jsfiddle.net/46wYS/
使用寬鬆的正則表達式分裂更新演示:http://jsfiddle.net/46wYS/1/
var count = $('#foo').html().split(/<!--\s*boo\s* -->/i).length - 1;
+1不會想到這個..聰明:) – andr
看起來它應該工作,但即使在html源代碼中有幾個匹配,我仍然從alert(count)中獲得0。 –
確保您搜索的註釋字符串**完全**與HTML中的相同。我的意思是介於兩者之間的空格,或者使用更寬容的正則表達式變體。 – techfoobar
評論不是DOM和jQuery的部分不會找到它們。 – techfoobar