jquery - 在 jQuery 中是否有使用 if 或 case 的替代方法?

标签 jquery

我大约有10张图片,当 :hover每张图片都有一个ajax.load那将是触发器。我要做 10 jquery.hover ,但有件事告诉我有更好的解决方案。

我在想类似的事情

case 1 : load a
case 2 : load b
case 3 : load c
etc...

如何在 jquery 中执行此操作?

明白10张不同的图片加上10种不同的文字必须加载在<div>中所以现在我正在做

$('#a).hover.load('file' #tx-a');
$('#b).hover.load('file' #tx-b');
$('#c).hover.load('file' #tx-c');

最佳答案

这是一个简单的例子:

<p id="status">&nbsp;</p>
<img class="viewable" id="unique" src="foo.jpg"/><br/>
<img class="viewable" id="id" src="foo.jpg"/><br/>
<img class="viewable" id="for" src="foo.jpg"/><br/>
<img class="viewable" id="each" src="foo.jpg"/><br/>
<img class="viewable" id="element" src="foo.jpg"/><br/>

<script type="text/javascript">
  $(function() {
    var stuff = ['hello', 'world', 'you', 'look', 'nice'];

    $('.viewable').hover(
      // triggered when mouse moves onto element
      function() {
        var index = $('.viewable').index(this);

        // Replace this next line with a jQuery AJAX call. Pass the index 
        // as a parameter to whatever location you are pulling the text 
        // from, and have the AJAX callback stuff the text into the 
        // #status element. This is just a placeholder.
        $('#status').html('Hover IN, index ' + index + ' (' + stuff[index] + ')');
      },
      // triggered when mouse move off of element
      function() {
        // Optional: clear status element when mouse moves off image
        $('#status').html('&nbsp;');
      }
    );
  });
</script>

关于jquery - 在 jQuery 中是否有使用 if 或 case 的替代方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/718158/

相关文章:

jquery - 隐藏除第一个匹配元素之外的所有元素

php - 从谷歌地图读取 json 文件放置 api(使用 javascript 或 php)

java - 使用 AJAX 和 Java 注销不起作用

jquery - 在按钮上单击重定向到 url,并从输入框中抓取查询字符串

javascript - 如何使用 jQuery 动画从上到下滑动不同的 div?

javascript - 如何添加动画效果以在单击按钮时向右和向左滚动?

javascript - 向下滚动页面出现图像 | Sharepoint 在线嵌入

jquery - jQuery 中的缓动函数

javascript - 滚动到水平 div 中的元素

php - fancybox 上的图像翻转效果