【问题标题】:uncaught typeError cannot read property 'attributes' of undefined visual composer未捕获的 typeError 无法读取未定义的可视化作曲家的属性“属性”
【发布时间】:2016-08-10 12:44:48
【问题描述】:

我有一些视觉作曲家问题。 由于旧版本无法获得支持。客户不付款。 问题是我无法在后端添加元素。 chrome调试出错:

我已尝试使用以下代码解决问题: 未捕获的类型错误:无法读取未定义的属性“属性”

<pre>
html2element    @   composer-view.js?ver=4.7.4:156
render          @   composer-view.js?ver=4.7.4:163
addShortcode    @   composer-view.js?ver=4.7.4:232
addShortcode    @   composer-view.js?ver=4.7.4:561
_               @   load-scripts.php?c=0&load[]=thickbox,hoverIntent,common,admin-bar,word-count,suggest,wp-ajax-respon…:474
m               @   load-scripts.php?c=0&load[]=thickbox,hoverIntent,common,admin-bar,word-count,suggest,wp-ajax-respon…:474
f               @   load-scripts.php?c=0&load[]=thickbox,hoverIntent,common,admin-bar,word-count,suggest,wp-ajax-respon…:474
l.trigger       @   load-scripts.php?c=0&load[]=thickbox,hoverIntent,common,admin-bar,word-count,suggest,wp-ajax-respon…:474
ListenerHelper.triggerShortcodeEvents   @   events.js?ver=4.7.4:19
(anonymous function)    @   composer-view.js?ver=4.7.4:977
and alot fo load script errors
</pre>

代码:

html2element: function(html) {
var $template, attributes = {},
    template = html;
$template = $(template(this.model.toJSON()).trim()), _.each($template.get(0).attributes, function(attr) {
    attributes[attr.name] = attr.value
}), this.$el.attr(attributes).html($template.html()), this.setContent(), this.renderContent()
},

我已经检查了整个网络和 stackoverflow,我找不到这个问题的问题。

【问题讨论】:

  • 从错误中你在这行 $template.get(0).attributes 有问题。所以 $template.get(0) 返回未定义。确保使用 get 方法确实有结果。希望这会有所帮助。
  • 嗨,Mykola。当我记录 var $templets.get(0) 时,我在控制台中得到了一些 html 代码,这意味着它没有取消定义正确吗?不是。属性老了?不应该。属性?
  • 嗯..你有迭代那里_.each。也许第一个元素不是未定义的,但有些可能是.. 例如,总是在 _.each 之前尝试 console.log $template.get(0)。还将日志添加到所有变量。并检查是否您在 $template 中有您不期望的额外内容。希望这会有所帮助。
  • 如果我添加:$template = $(template(this.model.toJSON()).trim());console.log($template.get( 0 ).attributes), 控制台输出:NamedNodeMap {0: data-element_type, 1: class, length: 2} composer-view.js?ver=4.7.4:160 NamedNodeMap {0: data-element_type, 1: data-vc-column-width, 2: class, length: 3} 它看起来是正确的。你不觉得吗?

标签: javascript jquery json ajax wordpress


【解决方案1】:

最好的解决方案是将composer视图中的html2element代码和渲染代码替换为以下代码

html2element: function(html) {
            var attributes = {},
                $template;
            if (_.isString(html)) {
                this.template = _.template(html);
                $template = $(this.template(this.model.toJSON(), vc.templateOptions.default).trim());
            } else {
                this.template = html;
                                $template = $(this.template(this.model.toJSON(), vc.templateOptions.default).trim());
            }

            _.each($template.get(0).attributes, function(attr) {
                attributes[attr.name] = attr.value;
            });
            this.$el.attr(attributes).html($template.html());
            this.setContent();
            this.renderContent();

        },
        render: function() {
            var $shortcode_template_el = $('#vc_shortcode-template-' + this.model.get('shortcode'));
            if ($shortcode_template_el.is('script')) {
                this.html2element(_.template($shortcode_template_el.html()));
            } else {
                var params = this.model.get('params');
                $.ajax({
                    type: 'POST',
                    url: window.ajaxurl,
                    data: {
                        action: 'wpb_get_element_backend_html',
                        data_element: this.model.get('shortcode'),
                        data_width: _.isUndefined(params.width) ? '1/1' : params.width,
                        _vcnonce: window.vcAdminNonce
                    },
                    dataType: 'html',
                    context: this
                }).done(function(html) {
                    this.html2element(html);
                });
            }
            this.model.view = this;
            this.$controls_buttons = this.$el.find('.vc_controls > :first');
            return this;
        },

来源:https://gist.github.com/maximspokoiny/34ad60ad90944f8a80c6fc093873a807/9fb041d2b12249fe4391f986f4e7e6a08f57c6b3#file-gistfile1-txt

【讨论】:

  • 这对我有用。仅更改 html2element 给了我未定义的属性错误,但更改渲染函数解决了问题。
猜你喜欢
  • 2023-01-14
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 2022-12-21
  • 1970-01-01
  • 2019-05-16
  • 2018-05-28
  • 1970-01-01
相关资源
最近更新 更多