php - 在一些php的输出中添加一个id
问题描述
我有一个小代码,
我需要在输出中添加一个类
$output_html .= ' ' . groovy_menu_woocommerce_mini_cart_counter( $qty ) . ' ';
输出一个
<span class="gm-cart-counter">2</span>
如何在输出中添加 id
' . groovy_menu_woocommerce_mini_cart_counter( $qty ) . '
所以看起来像这样
<span id="newid"class="gm-cart-counter">2</span>
新代码测试
$span = groovy_menu_woocommerce_mini_cart_counter($qty);
$spanWithId = "<span id='the-id'" ;
该工作并显示带有-id id的跨度
但这不是
$span = groovy_menu_woocommerce_mini_cart_counter($qty);
$spanWithId = "<span id='the-id'" . $span;
解决方案
这很丑陋,但它应该可以解决问题:
$span = groovy_menu_woocommerce_mini_cart_counter($qty);
$spanWithId = "<span id='the-id'" . substr($span, 5, strlen($span));
$output_html .= $spanWithId;
echo "$spanWithId";
输出:
<span id='the-id' class="gm-cart-counter">2</span>
尝试这个
global $woocommerce;
$qty = 0;
if ($tks == true) {
$qty = $woocommerce->cart->get_cart_contents_count();
}
$cartIcon = 'fa fa-shopping-cart';
$span = groovy_menu_woocommerce_mini_cart_counter($qty);
$spanWithId = "<span id='the-id'" . substr($span, 5, strlen($span));
$output_html .= '
<div class="gm-minicart minicartmarie">
<a href="' . get_permalink( wc_get_page_id( 'cart' ) ) . '" class="gm-minicart-link minicartmarie">
<div class="gm-badge">
<i class="gm-icon ' . esc_attr( $cartIcon ) . '"></i>
' . $spanWithId . '
</div>
</a>
</div>
';
您正确替换了对的调用,groovy_menu_woocommerce_mini_cart_counter($qty)
但您;
在破坏它的语句中包含了 a 。
此外,您应该代替echo
ing$spanWithId
echo $output_html
推荐阅读
- android - ArrayList、Map.Entry 的问题
) 入口 - visual-studio - MFC:如何修复“待办事项”
(32位)”在任务管理器输出? - python - 如何将 str.format() 与带有一些乳胶的降价文件一起使用
- python - 以宽格式制作 pandas df 并将值取消连接到不同的列
- mongodb - 文档数据库中的分支和合并功能?
- javascript - 如何知道反应组件在整个现有反应应用程序中被导出到哪些文件中?
- c++ - vector.erase 指针上的内存垃圾
- javascript - 声明为变量名时未定义在浏览器中引发错误,但在 node.js 中运行时成功运行
- javascript - JavaScript 运算符中的意外输出
- django - 模型保存方法中的Django多对多更改不起作用