我想使用JQuery從HTML字符串訪問元素。使用jQuery從HTML js對象中獲取控件/訪問元素
我有一個以下的AJAX函數,它在成功時返回HTML文本。我希望僅使用Jquery從HTML文本中提取標記的內容。
假設我有一個包含HTML內容的變量resp
。我想訪問下面這個HTML裏面的內容標籤的內容..
var resp = "<html><head></head><body><form action="changePassword" name="fmChangePassword"> .... form elements .... </form></body> </html>"
alert(resp); //this alerts the full HTML content properly
alert($(resp).find("form").html()); //trial 1 -- returns NULL
var test = $(resp).find("#changePassword"); //#changePassword is the ID of the form
//alert(test.html());
displayChangePwdWindow(test); //need to pass the form content here
我嘗試使用.find()
,但沒有成功。
任何人都可以幫助我如何做到這一點?
你到底想幹什麼?將在[jsfiddle](http://jsfiddle.net)中給出完整的代碼? – yosafatade
嘗試提醒resp,你在那裏得到的HTML? – rahul
你能顯示你的resp內容嗎?可能有些事情是錯的。因爲這應該工作得很好:http://jsfiddle.net/cEBsF/ –