如何改变Javascript中的字符值?

发布于 2024-08-24 08:05:08 字数 1238 浏览 4 评论 0原文

我这里有这段代码,

{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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留蓝 2024-08-31 08:05:08

看起来你想使用 smarty 迭代计数器来设置 id。请查看此处的示例 7-11:http://www. smarty.net/manual/en/language.function.foreach.php

{* this will output sticky1, sticky2, sticky3, ... etc *}

{foreach from=$cart.cartItems item="item" name="cart"}
    sticky{$smarty.foreach.cart.iteration},
{/foreach}

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

{* this will output sticky1, sticky2, sticky3, ... etc *}

{foreach from=$cart.cartItems item="item" name="cart"}
    sticky{$smarty.foreach.cart.iteration},
{/foreach}
笛声青案梦长安 2024-08-31 08:05:08

foreach 循环中生成代码时更改它比稍后使用 JavaScript 修复它要有效得多。请注意,如果有人禁用了 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="sticky{$item.Product.ID}">{$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="sticky{$item.Product.ID}" class="atip" style="width:200px;">

...

使用 javascript(以及稍后使用 jQuery 实现相同的功能)。

   $(function() {
         $('[data-tooltip=sticky1]').each( function(i) {
              var tipNo = i + 1;
              $(this).attr('data-tooltip','sticky' + i);
         });
   });

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:

{foreach from=$cart.cartItems item="item" name="cart"}    

<div id="cart2Produkt">


 <p>{if $item.Product.ID}
                    <a href="{productUrl product=$item.Product}" data-tooltip="sticky{$item.Product.ID}">{$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="sticky{$item.Product.ID}" class="atip" style="width:200px;">

...

Using javascript (and jQuery to achieve the same thing later).

   $(function() {
         $('[data-tooltip=sticky1]').each( function(i) {
              var tipNo = i + 1;
              $(this).attr('data-tooltip','sticky' + i);
         });
   });

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文