从javascript输出中移除HTML标签 [英] Remove HTML tags from javascript output

查看:85
本文介绍了从javascript输出中移除HTML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了看周围的计算器,但我找不到解决我的问题的答案。我希望我能解释一下,所以你们都明白我要做的事......(如果需要更多信息,请告诉我。)



我有一个PHP脚本,用于添加和删除会话数组中的内容。现在这是这个脚本的代码:

 <?php foreach($ _SESSION ['products'] as $ product ):?> 
< p style =font-size:1.2rem><?php echo htmlspecialchars($ product); ?>< / p为H.
<?php endforeach;?>

使用Javascript我将其回显为 textarea 返回值的地方。



Javascript

 <$ c $('。my-subject')。click(function(){
var title = $(this).attr('value');
$(textarea#thema) .html(数据);
})

现在它返回带有HTML的值标签< p> ... < / p>
我怎样才能让它在最后没有HTML标签和换行符的情况下返回值?我尝试过使用:

  $(textarea#thema)。text()+'\\\
';

但这并不奏效,这仍然显示< p>



我还看到了一些这样的代码:

  function removeTags(){
var txt = document.getElementById('myString')。value;
var rex = /(<([^>] +)>)/ ig;
alert(txt.replace(rex,));
}

但我不知道我是否可以将它用于我的代码以及如何要做到这一点...任何帮助将不胜感激。谢谢。

解决方案

听起来像数据是一个包含标记的字符串表格< p style =font-size:1.2rem> STUFF HERE< / p> ,重复,你只想获得<$ c $



可靠的方法是解析HTML并从中提取文本:



  // datavar data ='< p style =font-size:1.2rem > First< / p>'+'< p style =font-size:1.2rem> Second< / p>'+'< p style =font-size:1.2rem> Third< ; / p>'; //解析itvar elements = $ .parseHTML(data); //将其转换为entriesvar的文本数组= entries.map(function(element){return $(element).text ();}); //获取与连接的数组条目的字符串\\ nvar value = entries.join(\\\
); //完成!$(#output).val(value);

 < textarea id =outputrows =4>< / textarea>< script src =https: //ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js\"></script>  



可以更简洁地编写,但为了清晰起见,我将各部分分开。


I looked around stackoverflow but I couldn't find an answer that solved my problem. I hope I can explain it so you all understand what I'm trying to do... (If more info is needed, please let me know).

I have a piece PHP script that adds and removes content from an session array. Right now this is the code for this script:

<?php foreach ($_SESSION['products'] as $product): ?>
    <p style="font-size:1.2rem"><?php echo htmlspecialchars($product); ?></p>
<?php endforeach;?>

Using Javascript I echo this to a textarea where it returns the values.

Javascript

$('.my-subject').click(function(){
    var title = $(this).attr('value');
      $("textarea#thema").html(data); 
})

Right now it returns the value with the HTML Tags <p>...</p>. How can I get it to return the value's with no HTML tags and a line break at the end? I tried using:

$("textarea#thema").text() + '\n';

But that didn't work, this still shows the <p>-tags and no line break.

I also saw some codes like this:

function removeTags(){
    var txt = document.getElementById('myString').value;
    var rex = /(<([^>]+)>)/ig;
    alert(txt.replace(rex , ""));
}

But I don't know if I can use that for my code and how to do this... Any help would be appreciated. Thanks.

解决方案

It sounds like data is a string containing markup in the form <p style="font-size:1.2rem">STUFF HERE</p>, repeated, and you want to get just the STUFF HERE part.

The reliable way to do that is to parse the HTML and extract the text from it:

// The data
var data =
    '<p style="font-size:1.2rem">First</p>' +
    '<p style="font-size:1.2rem">Second</p>' +
    '<p style="font-size:1.2rem">Third</p>';
// Parse it
var elements = $.parseHTML(data);
// Convert that to an array of the text of the entries
var entries = elements.map(function(element) {
  return $(element).text();
});
// Get a string of the array entries joined with "\n"
var value = entries.join("\n");
// Done!
$("#output").val(value);

<textarea id="output" rows="4"></textarea>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

That can be written more concisely, but I kept the parts separate for clarity.

这篇关于从javascript输出中移除HTML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆