フレックスコンテナに要素を動的に行単位で追加する必要がある純粋なjavascriptを作成しています。私の驚いたことに、私のmouseoverイベントは行を横切って伝播してはいけませんが、他の子をトリガします。以下は私のコードです。mouseoverイベントのバブリングの問題 - Javascript
function drawChildren() {
var size = Math.floor(containerSize/childSize);
var counter = 1;
var parent = document.getElementById(parentId);
for(var rowCount = 1; rowCount <= size ; rowCount ++) {
var row = document.createElement('div');
row.id = `${parentId}-rowDiv-${rowCount} `;
row.setAttribute('style', `
height: ${childSize}px;
width: ${containerSize}px;
display: flex;
flex-direction:row; `);
for(var child = 1; child <= size ; child ++) {
var childDiv = document.createElement('div');
childDiv.id = `${parentId}-childDiv-${counter}`;
childDiv.setAttribute('style',`
height: ${childSize}px;
width: ${childSize}px;
background-color: ${getRandomColor()};`);
childDiv.addEventListener("mouseover", onMouseOver);
childDiv.addEventListener("mouseleave", onMouseLeave);
row.appendChild(childDiv);
counter ++;
}
parent.appendChild(row);
}
のonmouseover、iは物体上にマウスオーバーするたびに問題が、それは行を横切って伝播さ
function onMouseOver(e) {
e.stopPropagation();
document.getElementById(e.target.id).style.display = 'none';
console.log(e.target.id);
}
以下の関数を呼び出し、同じ上の他のすべての項目のマウスオーバーイベントを発生します行。一度に1つずつ発射します。 js stopPropagation()
propを追加して伝播を止めようとしましたが、何も変わりません。これを引き起こしているのはどうですか?私はそれにどのように対処していますか?どんな助けもありがとう。
おかげで男:ここ
は(
display: none;
とvisibility : hidden;
付き)あなたのコードの作業抜粋です。 '.style.visibility = 'hidden';'は私のために働きます。あなたの時間をありがとう。 – brickleberry