开发者

jQuery- Using a variable as a selector?

In this fid开发者_运维百科dle, 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."

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

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()); 
});


Like this: http://jsfiddle.net/Paulpro/5L8Q8/54/

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


Here it is:

$('#button').click(function() { 
  ran = getRandom(myArray, true); 
  $('#result').html($('#'+ran).html());
});

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜