使用document.getElementById()警告字符串.innerHTML

时间:2015-10-16 20:23:31

标签: javascript php jquery html ajax

我尝试使用document.getElementById()。innerHTML来警告字符串,但它警告整个页面的代码,而不是div中的字符串。我需要它来提醒' 121'。我做错了什么?

<script type = "text/javascript"> 

function getUrban(pageNum, stopAt, gotoUrl) {

var currentPage = "currentPage";
$.ajax({
           type: "POST",
           url: gotoUrl,
           data: { pageNum : pageNum },
           error: function(xhr,status,error){alert("error");},
           success:function(data) {
           document.getElementById("output").innerHTML = data;
           currentPage = document.getElementById("output").innerHTML;
           },
           complete:function(data) {
           alert(currentPage);
           } //end of complete:function(data)

      });

} //end of function getUrban(pageNum)

getUrban(121,422,"test.php");

</script>

<div id = "output"></div>

警报输出:

The full code of the whole page, plus some more code about setting the width.

以id&#39;输出&#39;:

的div输出
121

需要提醒:

121

test.php

$pageNum = $_POST['pageNum'];
echo $pageNum;

1 个答案:

答案 0 :(得分:-1)

由于您使用jQuery来执行ajax,为什么不使用jquery来处理其他事情。

$('#output').text();

只获取div中的文本。不是html元素。

让我疯狂的是看到人们使用jQuery并在他们的代码中使用document.getElementById(&#39; id&#39;)。你会意识到$(&#39; id&#39;)将获得相同的元素,但使用jQuery包装器,因此你可以使用jQuery函数。打字时间要短得多。

<script type = "text/javascript"> 

function getUrban(pageNum, stopAt, gotoUrl) {

$.ajax({
           type: "POST",
           url: gotoUrl,
           data: { pageNum : pageNum },
           error: function(xhr,status,error){alert("error");},
           success:function(data) {
               $("#output").html(data);
           },
           complete:function(data) {
               alert($('#output').text());
           } //end of complete:function(data)

      });

} //end of function getUrban(pageNum)

getUrban(121,422,"test.php");

</script>

<div id = "output"></div>
相关问题