Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 聚合:在index.html中未标识dom重复项上的单击事件_Javascript_Html_Polymer_Polymer 2.x_Dom Repeat - Fatal编程技术网

Javascript 聚合:在index.html中未标识dom重复项上的单击事件

Javascript 聚合:在index.html中未标识dom重复项上的单击事件,javascript,html,polymer,polymer-2.x,dom-repeat,Javascript,Html,Polymer,Polymer 2.x,Dom Repeat,我有一个带有入口点index.html的聚合物应用程序。出于某种原因,我不得不在index.html本身中使用dom repeat,而不是聚合元素。代码是这样的 <dom-bind id="mainbody"> <template> <app-drawer-layout> <app-drawer slot="drawer"> <template is="d

我有一个带有入口点index.html的聚合物应用程序。出于某种原因,我不得不在index.html本身中使用dom repeat,而不是聚合元素。代码是这样的

    <dom-bind id="mainbody">
    <template>
        <app-drawer-layout>
            <app-drawer slot="drawer">
                <template is="dom-repeat" id="mainDemoBody">
                    <paper-item data-value={{item.is}} id="demoItem" on-tap="onElementSelect"> 
                      {{item.is}}         
                    </paper-item>
                </template>
            </app-drawer>
            <div> Main content
                <div>
        </app-drawer-layout>
    </template>
</dom-bind>
    <script>
    function onElementSelect(e) {
        console.log('here');
        this.selectedElement = e.model.item;
        this.elementTags = this.selectedElement.tags;
        this.demoLoaded = false;
    }
</script>

{{item.is}}
主要内容
我在脚本标签中定义了如下的点击功能

    <dom-bind id="mainbody">
    <template>
        <app-drawer-layout>
            <app-drawer slot="drawer">
                <template is="dom-repeat" id="mainDemoBody">
                    <paper-item data-value={{item.is}} id="demoItem" on-tap="onElementSelect"> 
                      {{item.is}}         
                    </paper-item>
                </template>
            </app-drawer>
            <div> Main content
                <div>
        </app-drawer-layout>
    </template>
</dom-bind>
    <script>
    function onElementSelect(e) {
        console.log('here');
        this.selectedElement = e.model.item;
        this.elementTags = this.selectedElement.tags;
        this.demoLoaded = false;
    }
</script>

函数OneElementSelect(e){
console.log('here');
this.selectedElement=e.model.item;
this.elementTags=this.selectedElement.tags;
this.demoLoaded=false;
}
但我在用户界面上点击任何dom项时都会出现以下错误

侦听器方法
onElementSelect
未定义

有人能帮我一下吗,提前谢谢。

onElementSelect”似乎没有与dom绑定#主体绑定。 我的建议是创建一个与主体绑定的函数。这似乎对我有用

代码:-

var mainbody = document.getElementById('mainbody');
mainbody.onElementSelect = function(e){
        console.log('here');
        this.selectedElement = e.model.item;
        this.elementTags = this.selectedElement.tags;
        this.demoLoaded = false;
}

编辑:请注意,提供的溶液不适合或不必要用于聚合物元件。此场景中需要此特定修复,因为聚合物组件正在html文件中使用。

这很可能与您的问题无关,但在使用
id=“demoItem”
实例化纸张项目时,您将遇到重复id问题。此外,我认为Polymer 2需要在函数声明中使用冒号<代码>函数:onElementSelect(){}这不必用@Rahul Kumar建议来解决。这是关于范围的问题。按照Rahul的建议,在dom绑定范围内编写函数是可行的。我同意@Niklas,对于纯聚合物元素来说,这是不必要的。但实际情况是,它是一个html文件,我们在其中绑定聚合元素。我将我的解决方案视为一种将脚本方法与dom绑定的方法,以便事件能够工作。任何替代方案都会有帮助。一定是错过了dom绑定,但我无法消除不喜欢,直到文章被编辑。