javascript - 为什么我从 jQuery 和 HttpHandler 获取 html 标签?

标签 javascript java jquery html httpserver

我有以下代码:

 <script>
 $(document).on('click','#Save',function()
 {
   $.get('My_App?New_Info='+$('button[id^=\"Current_\"]').text(),function(responseText)
   {
     $('#Page_div').text(responseText);
   });
 });
 </script>

html 看起来像这样:

<div id=Page_div>
      <Table Border=1>
        <Tr>
          <Td><button id=Current_1 type=button></button></Td>
          <Td><button id=Current_2 type=button></button></Td>
          <Td><button id=Current_3 type=button></button></Td>
          <Td><button id=Current_4 type=button></button></Td>
          <Td><button id=Current_5 type=button></button></Td>
          <Td><button id=Current_6 type=button></button></Td>
          <Td><button id=Save type=button>Save</button></Td>
        </Tr>
      </Table>
</div>

还有HttpHandler代码如下:

  public void handle(HttpExchange exchange)
  {
    OutputStream responseBody=exchange.getResponseBody();
    String requestMethod=exchange.getRequestMethod(),requestPath=exchange.getRequestURI().getPath(),title="ABC",
             New_Info,responseString="<Html>\n<Head>\n  <Title>"+title+"</Title>\n</Head>\n<Body BgColor=#FDF5E6>\n<Center>\n"+Get_jQuery()+"\n\n";
    LinkedHashMap<String,String> params=queryToMap(exchange.getRequestURI().getRawQuery());
    File file;

    try
    {
      New_Info=(params.get("New_Info")==null?null:URLDecoder.decode(params.get("New_Info"),"utf-8"));

      if (New_Info!=null)                                                                    // After Save button is clicked, save new user info
      {
        Headers responseHeaders=exchange.getResponseHeaders();
        responseHeaders.set("Content-Type","text/html;charset=utf-8");
        exchange.sendResponseHeaders(200,0);
        responseString="<P><H2>Info [ "+New_Info+" ] saved.</H2>";
        responseBody.write(responseString.getBytes());
      }
...
}

一切都很好,除了一件事,按钮被替换为 HttpHandler 中的新文本。 ,这是字符串:"<P><H2>Info [ xx ] saved.</H2>"

但我希望它是大字体,并且屏幕上的文本应该是:“信息 [xx] 已保存。”而不是向用户显示 html 标签的 RAW 未渲染版本,为什么?我也尝试过:

responseHeaders.set("Content-Type","text/plain");

没区别,我也尝试输入<html> ... </Html>围绕它,仍然不起作用,修复它的正确方法是什么?

最佳答案

text() 表示“将内容视为纯文本”。

如果您想将其视为 HTML 源代码,请改用 html()

关于javascript - 为什么我从 jQuery 和 HttpHandler 获取 html 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35281213/

相关文章:

javascript - 如何在渲染函数中附加下拉选项?

java - Android线程时间,utime?

java - 正确有效地按列阅读元素的方法

javascript - 具有复杂 json 的 CORS 兼容 $.ajax 后调用

javascript - 使用谷歌翻译进行多语言文本区域

javascript - 来自background.js的Chrome扩展: Call function in contentscript. js

javascript - 二叉搜索树 JavaScript 实现 - 移除函数

java - Spring - 从对象池请求作用域 bean

php - 当表单页面最小化时,如何将条形码读取器中的值插入到表单输入中?

javascript - 使用 nivoSlider,如何确定当前幻灯片图像或索引?