首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用短代码从localStorage中获取值

如何使用短代码从localStorage中获取值
EN

Stack Overflow用户
提问于 2021-10-19 01:47:16
回答 1查看 383关注 0票数 1

问题:

我需要创建一个包含从强标记中提取的值的短代码。

当我回音"document.write(localStorage.getItem('getnum'));";

代码显示值85。

但是当我返回它(下面的代码)时,短代码显示点没有显示值。

请让我知道如何返回这个价值好吗?

我尝试过的代码:

代码语言:javascript
复制
    <?php
         ?>
    <script type="text/javascript">
      jQuery(document).ready(function($) {
        let getnum = $('div.elementor-widget-container p > strong').text();
        JSON.parse(localStorage.getItem('getnum'));
      });
    </script>

    <?php
    function points_show_function() {
    return "<script>document.write(localStorage.getItem('getnum'));</script>";
    }
    add_shortcode('showpoints', 'points_show_function');

控制台:

无差错

HTML (代码来自yith点&奖励插件):

代码语言:javascript
复制
<div class="elementor-element elementor-element-c1d0790 elementor-widget elementor-widget-text-editor" data-id="c1d0790" data-element_type="widget" id="pointsid" data-widget_type="text-editor.default">
    <div class="elementor-widget-container">
        <p>Your credit is  <strong>85</strong> Points</p>                       
    </div>
</div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-19 03:38:09

  1. 您需要先设置值,以便稍后才能得到它!在在线6上,您需要设置它。
  2. 在回调函数中将return替换为echo
  3. 您需要在模板中使用do_shortcode来调用您的短代码!

所以你的代码应该是这样的:

代码语言:javascript
复制
<script type="text/javascript">
  jQuery(document).ready(function($) {
    let getnum = $('div.elementor-widget-container p > strong').text();
    localStorage.setItem('getnum', getnum);
  });
</script>

<?php
add_shortcode('showpoints', 'points_show_function');

function points_show_function()
{
  echo "<script>document.write(localStorage.getItem('getnum'));</script>";
}

do_shortcode('[showpoints]');

这个do_shortcode('[showpoints]');将在这里发挥魔力!因此,将其放在要输出值的模板上。

替代方式

根据Mozilla Docs,不需要使用JSON.stringify将单个字符串值设置为localStorage,但为了防止第一个方法无法工作,请使用以下代码:

代码语言:javascript
复制
<script type="text/javascript">
  jQuery(document).ready(function($) {
    let getnum = $('div.elementor-widget-container p > strong').text();
    localStorage.setItem('getnum', JSON.stringify(getnum));
  });
</script>

<?php
add_shortcode('showpoints', 'points_show_function');

function points_show_function()
{
  echo "<script>document.write(JSON.parse(localStorage.getItem('getnum')));</script>";
}

do_shortcode('[showpoints]');

另一种使用return的方法

如果需要return回调函数中的值,请使用以下代码:

代码语言:javascript
复制
<script type="text/javascript">
  jQuery(document).ready(function($) {
    let getnum = $('div.elementor-widget-container p > strong').text();
    localStorage.setItem('getnum', JSON.stringify(getnum));
  });
</script>

<?php
add_shortcode('showpoints', 'points_show_function');

function points_show_function()
{
  return "<script>document.write(JSON.parse(localStorage.getItem('getnum')));</script>";
}

echo do_shortcode('[showpoints]');
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69624252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档