最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot read property 'contentDocument' of null - Stack Overflow

programmeradmin2浏览0评论

I have an SVG map that is niserted into HTML with help of the 'object' tag. Then, I try to use jQuery to access map's elements. (In particular I try to fill a rectangular in red.)

HTML:

<object data="map.svg" type="image/svg+xml" id="map" width="1840" height="940"></object>

jQuery:

jQuery(window).load(function () {
        var svgobject = document.getElementById('map');
        if ('contentDocument' in svgobject) {
            var svgdom = jQuery(svgobject.contentDocument);
            jQuery("#rect4578", svgdom).attr("fill", "red");
        }
    });

In chrome I get an error: Cannot read property 'contentDocument' of null (anonymous function) n.event.duspatch n.event.add.r.handle

What is the problem?

Thank you.

I have an SVG map that is niserted into HTML with help of the 'object' tag. Then, I try to use jQuery to access map's elements. (In particular I try to fill a rectangular in red.)

HTML:

<object data="map.svg" type="image/svg+xml" id="map" width="1840" height="940"></object>

jQuery:

jQuery(window).load(function () {
        var svgobject = document.getElementById('map');
        if ('contentDocument' in svgobject) {
            var svgdom = jQuery(svgobject.contentDocument);
            jQuery("#rect4578", svgdom).attr("fill", "red");
        }
    });

In chrome I get an error: Cannot read property 'contentDocument' of null (anonymous function) n.event.duspatch n.event.add.r.handle

What is the problem?

Thank you.

Share Improve this question edited Feb 5, 2015 at 7:36 plywoods asked Feb 5, 2015 at 7:07 plywoodsplywoods 2571 gold badge3 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

Are you getting this error only in chrome? Debug the code and look at this line in your code

var svgobject = document.getElementById('map')

What do you get in return here? Is the svgobject null? Because your id is not "map" its "imap"

The id is imap not map for getElement....

发布评论

评论列表(0)

  1. 暂无评论