问题:
我需要创建一个包含从强标记中提取的值的短代码。
当我回音"document.write(localStorage.getItem('getnum'));";
代码显示值85。
但是当我返回它(下面的代码)时,短代码显示点没有显示值。
请让我知道如何返回这个价值好吗?
我尝试过的代码:
<?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点&奖励插件):
<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>发布于 2021-10-19 03:38:09
6上,您需要设置它。return替换为echo。do_shortcode来调用您的短代码!所以你的代码应该是这样的:
<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,但为了防止第一个方法无法工作,请使用以下代码:
<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回调函数中的值,请使用以下代码:
<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]');https://stackoverflow.com/questions/69624252
复制相似问题