简体   繁体   English

为什么我使用 JQuery 得到的只是文本而不是 HTML?

[英]Why i'm getting just text instead of HTML using JQuery?

I have a H1 tag & i want to get HTML() of this element on button click but right now getting just text instead of HTML.我有一个 H1 标签,我想在单击按钮时获取此元素的HTML() ,但现在只获取文本而不是 HTML。 How can i get proper HTML?我怎样才能得到正确的 HTML?

My Code:-我的代码:-

 $(function(){ $('button').click(function(){ alert($('h1').html()); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1 style="color:indianRed">Heading 1</h1> <button>Click</button>

You should use outerHTML :你应该使用outerHTML

The outerHTML attribute of the Element DOM interface gets the serialized HTML fragment describing the element including its descendants. Element DOM 接口的outerHTML属性获取描述元素及其后代的序列化 HTML 片段。 It can also be set to replace the element with nodes parsed from the given string.它还可以设置为用从给定字符串解析的节点替换元素。

 $(function(){ $('button').click(function(){ alert($('h1').get(0).outerHTML); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1 style="color:indianRed">Heading 1</h1> <button>Click</button>

outerHTML in javascript javascript中的outerHTML

 $(function(){ $('button').click(function(){ alert(document.querySelector("h1").outerHTML); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1 style="color:indianRed">Heading 1</h1> <button>Click</button>

The .html() function return the inner html of an element, to get the outerHTML of any given element, try to wrap it inside a container then get his html using .html() ..html()函数返回内部HTML元素,获得outerHTML任何给定的元素,试图将其包装容器内,然后用得到他的HTML .html()

$(function(){
  $('button').click(function(){
        var elHTML = $('<div></div>').append($('h1'));
       alert(elHTML.html());
   });
 });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我使用jQuery获取[object object]? - Why i'm getting [object object] using jQuery? 为什么我无法使用jQuery将HTML动态添加到页面? - Why I'm not able to dynamically add the HTML to the page using jQuery? I'm using AJAX to post an object from javascript to php within the same page and I'm getting the entire html page instead of the object - I'm using AJAX to post an object from javascript to php within the same page and I'm getting the entire html page instead of the object 我在文档上使用jquery keydown事件获取了(TypeError jQuery),它引发了很多事件,而不仅仅是选定的事件 - I'm getting a (TypeError jQuery) using jquery keydown events on the document, its causing lots of events to fire rather than just the selected one 如何使用jQuery和JavaScript仅返回数据而不是HTML? - How to return just data instead of html using jquery and javascript? 有人可以解释为什么我得到 9 和 5 而不是 36880 和 375 - Can someone explain why i'm getting 9 & 5 instead of 36880 & 375 我在ASP.NET应用程序中使用ajax调用,并且从jquery Ajax调用中获取了整个HTML页面作为响应 - I'm using ajax call in ASP.NET application and I'm getting entire HTML page as response from jquery Ajax call 重构jQuery代码时,为什么会得到[object Object]而不是预期的HTML? - Why am I getting [object Object] instead of my expected HTML when refactoring jQuery code? 使用jQuery在嵌套的HTML标签中获取文本 - Getting text in nested html tag using jquery 作为一名精通HTML和CSS的设计师,开始使用jquery插件而不是学习javascript是否安全? 我不是程序员 - As a designer who has become proficient in HTML and CSS, is it safe to start using jquery plugins instead of learning javascript? I'm not a programmer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM