0

完全的新手,试图创建一个个性化的圣经,用户可以通过直接单击所述单词/短语来循环各个单词/短语的替代翻译。我知道我可能需要在函数中使用某种“for 循环”,但我希望将它们的首选项存储(作为字符串?)在缓存和用户配置文件中,因此文本不应在页面加载时重置为默认值。理想情况下,我会在 HTML 中嵌入备用文本选项(“数组”?),如下所示,同时保持 HTML 尽可能整洁。JS 最终会填充数千个 ID,可能每节圣经都有几个 ID,因此脚本越通用越好。提前致谢!

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.js"></script>
<script>
$(document).ready(function() {
    $('.alternate').click(function() {
        $('#1Cor13-4a').text('[What goes here if I want to cycle through the options listed in HTML?]');
        $('#1Cor13-4b').text('[Ditto]');
    });
});
</script>

My <span class="alternate" id="1Cor13-4a" onclick="ChangeText({"care","love"})">love</span><span class="alternate" id="1Cor13-4b" onclick="ChangeText({"is patient with","suffers long for"})">is patient with</span> you.
4

1 回答 1

1

使用数据属性...

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.js"></script>
<script>
$(document).ready(function() {
 $('.alternate').click(function() {
       var index=   $(this).attr("data-text-index");
     var text = $.parseJSON($(this).attr("data-text"));
     if(index== null)
         index = -1;
     index = Number(index)+1>=text.length? 0:Number(index)+1;
     $(this).html(text[index]);
     $(this).attr("data-text-index",index);
  });
});
</script>
My <span class="alternate" id="1Cor13-4a" data-text='["care","love"]'>love</span><span class="alternate" id="1Cor13-4b" data-text='[" is patient with"," suffers long for"]'> is patient with</span> you.
于 2013-02-05T23:57:23.283 回答