jQuery-使用变量作为选择器? [英] jQuery- Using a variable as a selector?

查看:251
本文介绍了jQuery-使用变量作为选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此小提琴中,如果您单击 http://jsfiddle.net/5L8Q8/52/黑框,它将从数组中随机选择一个"或两个",并将该值分配给运行".

In this fiddle, http://jsfiddle.net/5L8Q8/52/ if you click the black box, it randomly selects either "ONE" or "TWO" from an array and assigns that value to "ran."

在一个"或两个"页面上还有两个div,每个文本中都有内容.这些div与从数组中选择的值无关

There are also two divs on the page "ONE" or "TWO" with text content in each. These divs are unrelated to the values selected from the array

根据分配给ran的值,我想在div结果"中表示div"ONE"或"TWO"的文本内容...

Depending on the value assigned to ran, I want to express the text contents of div "ONE" or "TWO" inside div "result"...

我在下面玩类似的游戏,但是没有进展

I was playing around with something like this below but made no progress

$('#result').html(function() {
  //some function to put contents of either div ONE or TWO (depending on ran) inside of #result
});

推荐答案

http://jsfiddle.net/5L8Q8 /57/

$("#button").click(function() {
    ran = getRandom(myArray, true);

    $('#result').html($("#" + ran).html()); 
});

这篇关于jQuery-使用变量作为选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆