document.getElementById 使用 SMARTY 变量

document.getElementById with SMARTY Variable

我需要从具有 SMARTY 变量作为 ID 的 div 中获取 .innerHTML

看起来像这样:

<div id="{$products_data.IMAGE}">{$products_data.QTY}</div>

到目前为止我尝试过但由于某种原因无法正常工作的是:

test = document.getElementById("{$products_data.IMAGE}").innerHTML;

有人对 SMARTY 和 Javascript 有任何经验吗?

在 .tpl 文件中使用全局 JavaScript 变量来传递来自 Smarty 的值。

.tpl 文件中:

<script>
// Global variables 
var smartyDivId = "{$products_data.IMAGE}";
</script>

.js 文件中:

if (typeof smartyDivId != undefined) {
    test = document.getElementById(smartyDivId).innerHTML;
}