mdl-menu中的mdl-menu material-design-lite

时间:2016-10-08 00:33:48

标签: javascript html css material-design material-design-lite

我正在尝试在mdl-textfield内使用material-design-lite mdl-menu

问题是,当我打开菜单并单击文本字段(<input>)时,它会关闭菜单。

有人知道如何在不手动重新创建/设置菜单的情况下阻止这种情况吗?

以下是示例代码:

&#13;
&#13;
#button {
  margin-left:20px;
  margin-top:20px;
}
&#13;
<script src="https://code.getmdl.io/1.2.1/material.min.js"></script>
<link href="https://code.getmdl.io/1.2.1/material.indigo-pink.min.css" rel="stylesheet"/>


<button class="mdl-button mdl-js-button mdl-color--blue mdl-color-text--white" id="button">
  Open Menu
</button>
<ul for="button" class="mdl-menu mdl-js-menu">
  <li>
    <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
        <input class="mdl-textfield__input" type="text" id="sample3">
        <label class="mdl-textfield__label" for="sample3">Clicking here will close me :(</label>
    </div>
  </li>
  <li>item</li> 
  <li>item</li> 
  <li>item</li>
</ul>
&#13;
&#13;
&#13;

更新

使用快速角度指令解决了这个问题:

.directive('stopPropagation', function() {
    return {
        restrict: 'A',
        link: function($scope, $element) {
            $element.on('click', function($event) {
                $event.stopPropagation();
            });
        }
    };
});

1 个答案:

答案 0 :(得分:2)

尝试为输入字段添加事件处理程序,以防止事件传播

document.querySelector('.mdl-textfield__input').addEventListener('click', function(e) {e.stopPropagation()});

请参阅下面的代码段。

&#13;
&#13;
#button {
  margin-left:20px;
  margin-top:20px;
}
&#13;
<script src="https://code.getmdl.io/1.2.1/material.min.js"></script>
<link href="https://code.getmdl.io/1.2.1/material.indigo-pink.min.css" rel="stylesheet"/>



<button class="mdl-button mdl-js-button mdl-color--blue mdl-color-text--white" id="button">
  Open Menu
</button>
<ul for="button" class="mdl-menu mdl-js-menu">
  <li>
    <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
        <input class="mdl-textfield__input" type="text" id="sample3">
        <label class="mdl-textfield__label" for="sample3">Clicking here will close me :(</label>
    </div>
  </li>
  <li>item</li> 
  <li>item</li> 
  <li>item</li>
</ul>
<script>document.querySelector('.mdl-textfield__input').addEventListener('click', function(e) {e.stopPropagation()});</script>
&#13;
&#13;
&#13;