【问题标题】:jquery showing html tags instead of formatting them when brought in using PHPjquery 在使用 PHP 引入时显示 html 标签而不是格式化它们
【发布时间】:2012-11-13 22:20:45
【问题描述】:

我的数据库中存储了一些数据,例如

<b>Availability:</b> 

然后,我使用 html_entity_decode 将其回显到 PHP 的“data-promo”属性中

<a href="" class="button" data-promo="<?php echo html_entity_decode($mytext); ?>"></a>

在我的 Jquery 文件中。我想点击上面的按钮,然后在 '#key' div 中显示文本。

$('.button).click(function(e) {
  e.preventDefault();
  promo = $(this).attr('data-promo');
  $('div#key').html(promo);
});

除了它在 '#key' div 中呈现和显示的值之外,这一切都有效

<b>Availability</b>

我只是希望它格式化输出,而不是显示标签。

【问题讨论】:

  • 请问您为什么要从数据库中加载按钮值?我会说这是一种资源密集型方法。另外,你不能将粗体属性添加到 .button 类吗?
  • 看看我的回答
  • 每个锚标签都是一个产品的链接。 data-promo 包含有关产品的信息。这样,当用户单击锚点并且促销文本立即出现而不是等待它加载时,它会给出即时响应。它是一块具有不同格式的文本,而不仅仅是粗体,这就是为什么我不能只添加一个类

标签: php jquery html entities custom-data-attribute


【解决方案1】:

您不想使用 PHP 解码并将其插入属性中。这将使您的 HTML 格式错误。保持编码并使用JS对其进行解码。所以试试这个:

$('div#key').html(promo).text();

【讨论】:

  • 您是否将您的 PHP 更改为:
【解决方案2】:

尝试以下方法:

$('div#key').text(promo);

【讨论】:

  • 当我尝试这样做时,它会将它们改回 html 实体
【解决方案3】:

开始,工作并检查

<?php

    // we got this from db, in your case
    $mytext = "&lt;b&gt;Availability:&lt;/b&gt"

?>

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Script Test</title>
    <script src="jquery.min.js"></script>
    <script>

    // example

        $(function() {
            $('.button').click(function(e) {
                e.preventDefault();
                promo = $(this).attr('data-promo');

                    $("#key").html(promo).text();

                });
        });

    </script>
</head>
<body>

    <a href="" class="button" data-promo="<?php echo html_entity_decode($mytext); ?>">Test</a>
    <div id="key"></div>

</body>
</html>

【讨论】:

  • 这仍然不起作用。我读到了一些关于 与它有关的东西。有人知道吗?
猜你喜欢
  • 2017-05-15
  • 2016-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多