显示一个div内容,显示另一个div

时间:2019-04-17 07:38:11

标签: jquery html

我的问题是关于HTMl和Jquery的。

我有两个div,Div 1Div 2。第1师有一些内容。

我使用js获取一个div内容,并在其他div中显示了该内容。但是它显示了未定义的错误。

if (activeIndex == 1) {
            $( document ).ready(function() {
            console.log("i am here");
            var shippingAdrres = document.getElementsByClassName('billing-address-details').innerHTML;
            document.getElementById('shipping-address-after-summary').innerHTML = shippingAdrres;

           });

        }

错误:

http://prntscr.com/nd235u

3 个答案:

答案 0 :(得分:1)

您最好完全使用jQuery:

if (activeIndex == 1) {
        $( document ).ready(function() {
        console.log("i am here");
        var shippingAdrres = $('.billing-address-details').html();
        $('#shipping-address-after-summary').html(shippingAdrres);

       });

    }

有关.html()的更多信息,请访问:http://api.jquery.com/html/

答案 1 :(得分:0)

document.getElementsByClassName 将返回一个数组,因此您必须使用索引进行选择。

if (activeIndex == 1) {
            $( document ).ready(function() {
            console.log("i am here");
            var shippingAdrres = document.getElementsByClassName('billing-address-details')[0].innerHTML;
            document.getElementById('shipping-address-after-summary').innerHTML = shippingAdrres;

       });

    }

答案 2 :(得分:0)

  

document.getElementsByClassName('billing-address-details')

返回一个数组,因此您无法在其上调用innerHTML,因此它返回let people = [ { name: "Adam", email: "adam@email.com", age: 12, country: "United States" }, { name: "Amalie", email: "amalie@email.com", age: 12, country: "Argentina" }, { name: "Estefanía", email: "estefania@email.com", age: 21, country: "Argentina" }, { name: "Adrian", email: "adrian@email.com", age: 21, country: "Ecuador" }, { name: "Wladimir", email: "wladimir@email.com", age: 30, country: "Ecuador" }, { name: "Samantha", email: "samantha@email.com", age: 30, country: "United States" }, { name: "Nicole", email: "nicole@email.com", age: 43, country: "Colombia" }, { name: "Natasha", email: "natasha@email.com", age: 54, country: "Ecuador" }, { name: "Michael", email: "michael@email.com", age: 15, country: "Colombia" }, { name: "Nicolás", email: "nicolas@email.com", age: 43, country: "Colombia" } ]; let multipleDemo = people.slice(1); multipleDemo.forEach(function (current) { console.log(current.name); });

至少尝试: undefined