0

我试图将一个变量的内容分配给另一个变量。我该怎么做呢?到目前为止,我已经尝试过:

var className = $($(this).attr("class"););

$('.content').html(className)

编辑:

请假设 "var className = $($(this).attr("class"););" 的最终值 是点 1

第 1 点包含:

编辑 2:(包含完整代码)

    <div class="content"><h1>testing hello world</h1><div class="position"> <div class="point1"> test<br> awewa<br> TEST <h1> test </h1> </div></div></div>

    <script type="text/javascript">
    var point1 = '<div class="content"><h1>testing hello world</h1><div class="position"> <div class="point1"> test<br> awewa<br> TEST <h1> test </h1> </div></div></div>';
    var className = "Broken";
    $('.content [class] [class]').click(function () {$(this).fadeTo(250, 0.25, function () {
    className = $(this).attr("class");
    $('.content').html(className)

    $(this).fadeTo(250, 1.00);

    });}
    );

    </script> 


var point1 = '<div class="content"><h1>testing hello world</h1><div class="position"> <div class="point1"> test<br> awewa<br> TEST <h1> test </h1> </div></div></div>';

"$('.content').html(className)" <这个的className需要包含point1中的字符串)

4

4 回答 4

1

好的,假设我理解清楚。您有一些 html 存储在一些变量中,这些变量与某个元素的类名相同,并且在单击该元素时。您想从该变量中获取 html 并将其作为内容分配给具有 classname 的另一个元素.content

$('.someClass').on('click',function(){
    var className = this.className; // Get the classname whic is a variable
   var content = window[className];
   if(content)
    $('.content').html(content); //Assuming variable is defined in the window Scope. 
});

拆分这个: -

window[className]<-- 获取窗口范围内定义的变量。

更新:-

根据您的代码更新尝试此操作。由于您要替换内容并且单击再次发生,您应该使用委托事件处理程序.on(),使用.live旧版本的 jquery。

   var className = "Broken";
   $(document).on('click', '.content [class] [class]', function () {
       $(this).fadeTo(250, 0.25, function () {
           className = this.className;
           $('.content').html(window[className]);
           $(this).fadeTo(250, 1.00);

       });
   });

演示

于 2013-05-21T03:41:04.593 回答
0

Edit:

It sounds like this is what you're trying to do

var className = $('.' + $(this).attr("class")).html();
$('.content').html(className);
于 2013-05-21T03:30:09.790 回答
0

Assuming 'this' represented a element (like if you are inside a $.each() callback then $(this).attr('class') will return all of the CSS classes assigned to that element. This is not the same as all the CSS rules that effect it only the value that appears in the 'class=""'. The outer $() that you have will hide the return value from that first assigment in your question. The second line will set the innerHTML of an element with the class "content" to the value that the first line returned.

于 2013-05-21T03:34:21.730 回答
0

除了看起来很好之外,代码中存在语法错误。

假设您要做的是在 element 中显示该元素的类属性this.content

var className = $(this).attr("class");
$('.content').html(className)

演示:小提琴

更新:
假设className是与对象关联的属性的名称,x那么您可以使用它x[className ]来获取值。
如果变量是在全局上下文中创建的,那么您可以使用它window[className]来获取值

var point1 = '<div class="content"><h1>testing hello world</h1><div class="position"> <div class="point1"> test<br> awewa<br> TEST <h1> test </h1> </div></div></div>';
jQuery(function($){
    var className = "Broken";
    $('.content').on('click', '[class] [class]', function () {
        $(this).fadeTo(250, 0.25, function () {
            className = $(this).attr("class");
            $('.content').html(window[className])
        });
    });
});

演示:小提琴

于 2013-05-21T03:35:58.047 回答