如何改变Javascript中的字符值?
我这里有这段代码,
{foreach from=$cart.cartItems item="item" name="cart"}
<div id="cart2Produkt">
<p>{if $item.Product.ID}
<a href="{productUrl product=$item.Product}" data-tooltip="sticky1">{$item.Product.name_lang|truncate:20}</a>
{else}
<span>{$item.Product.name_lang|truncate:20}</span>
</a>
{/if}
<div id="mystickytooltip" class="stickytooltip">
<div style="padding:5px;">
<div id="sticky1" class="atip" style="width:200px;">
<img src="{$item.Product.DefaultImage.paths.3}" alt="{$item.Product.name_lang|escape}"><br>
{$item.Product.name_lang}
</div>
</div>
<div class="stickystatus"></div>
</div>
</p>
<p>
{include file="order/itemVariations.tpl"}
{include file="order/block/itemOptions.tpl"}
{if $multi}
{include file="order/selectItemAddress.tpl" item=$item}
{/if}
</p>
</div>
{/foreach}
我想每次循环时将“sticky1”中的“1”更改为“2”(它是 foreach 的一部分)。
但运气不好..另外,我对 Javascript 有点陌生,不知道如何将其记录在我想要的地方。
I have this code here,
{foreach from=$cart.cartItems item="item" name="cart"}
<div id="cart2Produkt">
<p>{if $item.Product.ID}
<a href="{productUrl product=$item.Product}" data-tooltip="sticky1">{$item.Product.name_lang|truncate:20}</a>
{else}
<span>{$item.Product.name_lang|truncate:20}</span>
</a>
{/if}
<div id="mystickytooltip" class="stickytooltip">
<div style="padding:5px;">
<div id="sticky1" class="atip" style="width:200px;">
<img src="{$item.Product.DefaultImage.paths.3}" alt="{$item.Product.name_lang|escape}"><br>
{$item.Product.name_lang}
</div>
</div>
<div class="stickystatus"></div>
</div>
</p>
<p>
{include file="order/itemVariations.tpl"}
{include file="order/block/itemOptions.tpl"}
{if $multi}
{include file="order/selectItemAddress.tpl" item=$item}
{/if}
</p>
</div>
{/foreach}
I want to change the "1"in "sticky1" to a "2" everytime this loops (it's part of a foreach).
But no luck.. Plus I am kinda new to Javascript and don't know how to document write it where I want it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来你想使用 smarty 迭代计数器来设置 id。请查看此处的示例 7-11:http://www. smarty.net/manual/en/language.function.foreach.php
Looks like you want to use the smarty iteration counter to set the id. Have a look at Example 7-11 here: http://www.smarty.net/manual/en/language.function.foreach.php
在
foreach
循环中生成代码时更改它比稍后使用 JavaScript 修复它要有效得多。请注意,如果有人禁用了 javascript,您的代码将无法运行,并且不会进行更改。如果您在服务器端进行更改,那么它首先会正确生成。示例:
使用 javascript(以及稍后使用 jQuery 实现相同的功能)。
FWIW,使用 document.write() 只会将内容附加到文档中。您需要使用 jQuery(或其他框架)或纯 JavaScript 来导航 DOM,以查找要更改的元素并更新其属性。在我看来,使用框架通常比使用纯 JavaScript 编写相同的代码容易得多。
It's going to be much more effective to change it while you are generating the code in your
foreach
loop than to fix it later with javascript. Note that if someone has javascript disabled, your code won't run and the changes won't be made. If you make the change server-side, then it gets generated correctly in the first place.Example:
Using javascript (and jQuery to achieve the same thing later).
FWIW, using document.write() will only append things to the document. You need to navigate the DOM, either using jQuery (or another framework) or plain javascript to find the element you want to change and update it's property. Using a framework, IMO, is usually much easier than writing the same code with plain javascript.