何らかの理由で、jQuery html()関数を動作させることができません。私は次のことを試しました:
特定の div を検索します。
var div = $("#divId");
結果:効く
jQuery で使用できることをテストします。
alert($(div).attr("id"));
結果: 動作します。IDが提示されます
div 内の html を取得します。
alert($(div).html());
結果: 機能しません。空のアラート
innerHTML を使用して div 内の html を取得します。
alert(document.getElementById($(div).attr("id")).innerHTML);
結果: 動作します。div コンテンツはアラートに表示されます
実際のコード:
$(document).ready(function(){
var autocomp = namespace('mx.autocomp');
autocomp.overlay = (function() {
var formId;
var $searchValueComp;
var $searchResultComp;
function init(inFormId, inSearchValue){
formId = inFormId;
$searchValueComp = $("#"+inFormId).find("[id$="+inSearchValue+"]");
$searchResultComp = $("#"+inFormId).find("[id$=searchResult]");
}
function handleOverlay(){
var fn = window[formId + "OverlayWid"];
var result = document.getElementById($searchResultComp.attr("id")).innerHTML;
if($searchValueComp.val().length==0){
fn.hide();
}
// Test - This does not work as I get an empty alert
alert($searchResultComp.html());
// Edit1: New test, this works.
// When I use this javascript, I start with initializing the script from the page
// using the "init(inFormId, inSearchValue)" function. The handleOverlay() function
// is called through the "oncomplete='mx.autocomp.overlay.handleOverlay();'" of a
// p:remoteCommand that executes a search in the db and then update the div called
// "searchResultComp".
//
// Only reason I can think of why the code below works is that the jQuery object $test
// is created after the div called "searchValueComp" has been updated by p:remoteCommand.
// However, I don't understand why the jquery object "searchValueComp" wouldn't have
// access to the same content as it should look for it first when the html() function
// is called. Or is the content of the div searchValueComp set immediately when the
// object is created in the "init" function?
var $test = $("#"+formId).find("[id$=searchResult]");
alert($test.html());
// I need to check if the div: "searchResultComp" has any content.
// As I don't get $searchResultComp.html() to work, I'm forced to
// use the "getElementById" way instead.
if(result.length==0){
fn.hide();
}else{
fn.show();
}
}
function print(text){
$("#textCheck").prepend(text + '\n');
}
return {
handleOverlay: handleOverlay,
init: init
};
})();
});
私は何を間違っていますか?