如何更改ajax上的特定元素

How to change specific element on ajax ?

本文关键字:元素 何更改 ajax      更新时间:2023-09-26

我有ajax调用改变用户信息。我想从特定元素的响应和变化值中得到值

示例:需要修改的元素:

<div id="changeMe"><!-- New Value --> </div> 
Ajax调用:
    $.ajax({
    url: "?rr=profile",
}).success(function(response) {
});

如何更改"changeMe"元素的值ONLY ?

你可以这样做:

$.ajax({
url: "?rr=profile",
}).success(function(response) {
   $('#changeMe').html('Your new content');
});

这将改变ID为"changeMe"的元素。参见JQuery API

要获取值,可以使用相同的方法。例子:

var res = $('#someOtherElement').html();

变量res现在是元素的内容。

试试

$.ajax({
    url: "?rr=profile",
}).success(function(response) {
var r = $( response ).find( "#changeMe" );
    $( "#changeMe" ).html( r.html() );  
});

您可以执行以下操作。

$('#changeMe').html(response);

您可以根据需要使用.html().text()的jQuery方法(无论响应内容是HTML还是纯文本):

$.ajax({
  url: "?rr=profile",
}).success(function(response) {
  $('#changeMe').html(response);
});

.html(response)换成.text(response),如果这样对你更好。

在你的成功函数中,你可以像下面这样做

.success(function(response) {
   $("#changeMe").append("Your value");
});

如果您想更改文本,则:

$('#changeMe').text('new data from response');

如果你也想改变CSS:

$('#changeMe').css('property', 'attribute');