programing

외부 설정 방법jQuery를 사용한HTML

javajsp 2023. 3. 5. 09:28

외부 설정 방법jQuery를 사용한HTML

사용자 컨트롤이 있습니다.예:

<div id="divItem">
some html
</div>

ajax 요구는 서버에서 이 UC의 새로운 html을 반환합니다.예:

<div id="divItem">
    new html
</div>

오래된 html을 새로운 html로 바꾸고 싶습니다.내가 어떻게 그럴 수 있지?감사해요.

divItem도 반환하는 경우

$("#divItem").replaceWith("NEW HTML");

새 HTML을 그 자리에 놓거나 내부 HTML을 교체합니다.HTML, 같은 컨테이너를 얻었기 때문에:

$("#divItem").html($("NEW HTML").html());

divItem을 반환하지 않으면

새로운 html을 넣기만 하면 됩니다.

$("#divItem").html("NEW HTML");

replaceWith가 검색 대상인 것 같습니다.

$('#divItem').replaceWith(serverResponse);

AJAX 콜에서 DOM 요소로 데이터를 배치하려면.load().

$('#divItem').load('somePage.html');

하나의 아이템을 여러 아이템으로 대체하고 싶은 경우.다음 작업을 수행할 수 있습니다.

var item_1 = $('<div>').text('item 1');
var item_2 = $('<div>').text('item 2');
var item_3 = $('<div>').text('item 3');

// 1/.
// dont' use this way because it's hard to read
$('#divItem').prop('outerHTML', item_1.prop('outerHTML') + item_2.prop('outerHTML') + item_3.prop('outerHTML'));

// 2/.
// dont' use this way because it's same to the first's
$('#divItem')[0].outerHTML = item_1.prop('outerHTML') + item_2.prop('outerHTML') + item_3.prop('outerHTML');

// 3/.
// if you use this way, how can we continue replace "#divItem" with "item_2"?
var obj = $('#divItem').replaceWith(item_1);

// "replaceWith" returns an object which was replaced with "item_1"
// there is no way to continue with "item_2" and "item_3"
// sure, if you DON'T want to write a new line
item_1.after(item_2);

// or
item_2.insertAfter(item_1);

// 4/.
// if we write this, the result should be: "item 3item 2item 1"
$('#divItem').after(item_1).after(item_2).after(item_3);

// so, the correct **inline** solution should be:
$('#divItem').after(item_3).after(item_2).after(item_1).remove();

넌 그냥 필요하니까

$('#divItem').html('new html');

이건 단지 디바의 내부를 대신할 뿐이지HTML: http://api.jquery.com/html/

언급URL : https://stackoverflow.com/questions/8254703/how-to-set-outerhtml-with-jquery