admin管理员组文章数量:1356322
Below is my current code. And it is working fine, but I need the .code class element to display as plain text and not render as HTML.
jQuery:
$(document).ready(function( ) {
$('.code').hide();
$('.codeLink').toggle(
function() {
$(this).next('.code').fadeIn();
$(this).addClass('close');
},
function() {
$(this).next('.code').fadeOut();
$(this).removeClass('close');
}
); // end toggle
});
HTML:
<a class="codeLink" style="margin-bottom: 10px; display: block;" href="#">Get The Code</a>
<div class="code"><a class="benefitsQandA" href="#">Get an Instant Quote & Apply</a></div>
This section of the .code class should display on the screen exactly like this (in other words not rendered as HTML just as text):
<a class="benefitsQandA" href="#">Get an Instant Quote & Apply</a>
Below is my current code. And it is working fine, but I need the .code class element to display as plain text and not render as HTML.
jQuery:
$(document).ready(function( ) {
$('.code').hide();
$('.codeLink').toggle(
function() {
$(this).next('.code').fadeIn();
$(this).addClass('close');
},
function() {
$(this).next('.code').fadeOut();
$(this).removeClass('close');
}
); // end toggle
});
HTML:
<a class="codeLink" style="margin-bottom: 10px; display: block;" href="#">Get The Code</a>
<div class="code"><a class="benefitsQandA" href="#">Get an Instant Quote & Apply</a></div>
This section of the .code class should display on the screen exactly like this (in other words not rendered as HTML just as text):
<a class="benefitsQandA" href="#">Get an Instant Quote & Apply</a>
Share
Improve this question
asked Aug 29, 2011 at 17:33
OldWestOldWest
2,3857 gold badges42 silver badges62 bronze badges
3
- 1 Is it only the HTML rendering that's the problem? I don't exactly see how the toggling is involved. – pimvdb Commented Aug 29, 2011 at 17:35
- It's preferable to use a "null javascript" href over a hash ("#"), like this: href="javascript://" – Diodeus - James MacFarlane Commented Aug 29, 2011 at 17:40
- Toggling is NOT involved, but it was part of the broader scope to give the full code. – OldWest Commented Aug 29, 2011 at 17:40
4 Answers
Reset to default 3try this:
$(".code").text($(".code").html());
of course, if you are doing that to multiple divs, you would need to use .each:
$(".code").each(function(){
$(this).text($(this).html());
});
Use $(".code").text($(".code").html())
The proper way to handle this is to escape the code you don't want the browser to render, like this:
<a class="codeLink" style="margin-bottom: 10px; display: block;" href="#">Get The Code</a>
<div class="code"><a class="benefitsQandA" href="#">Get an Instant Quote &amp; Apply</a></div>
If you don't then you aren't guaranteed the correct output from the browser since JQuery's .html() tag is a wrapper for .innerHTML (See http://api.jquery./html/) which doesn't always return the exact same markup that you use.
You can take a look at a tool like the one at http://accessify./tools-and-wizards/developer-tools/quick-escape/ to do the escaping for you.
Wikipedia also has a reference of XML/HTML entity codes
Could you not do this from the server?
When the user hits the toggle button pass the html to the server, using php replace the < and > characters with <
and >
and return the html back to the page.
unless you are dealing with a very large amount of data this shouldn't take long to process. and this way you will get 100% exactly what the original markup is.
本文标签:
版权声明:本文标题:javascript - jQuery render HTML as plain text w toggle (toggle is working fine, but cannot covert the HTML to text) - Stack Over 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744062704a2584422.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论