1

以前にJavaScriptの問題で私を助けてくれてありがとう。私の現在の問題は、画像のonMouseOverとonMouseOutでそれぞれ新しいウィンドウを開いたり閉じたりする必要があることですが、新しいウィンドウonMouseOver == trueの場合、新しいウィンドウを閉じたくありません。

簡単な解決策があると確信していますが、画像のonMouseOut = "closeDetails();"をキャンセルする方法がわからないようです。ユーザーが新しいウィンドウにカーソルを合わせた場合。以下は私が扱っているコードのほとんどです。よろしくお願いします。

<body>
   <img  name="img1" id="img1" onMouseOver="windowDelay(this);"
           onMouseOut="closeDetails();" src="images/127.jpg" height="240" width="166"/>
</body>

<script language="JavaScript" type="text/javascript">

// This opens the movie details pop-up after an
// half second interval.
function windowDelay(thatImg)
{
    winOpenTimer = window.setTimeout(function() {openDetails(thatImg);}, 2000);
}


// This is the function that will open the
// new window when the mouse is moved over the image
function openDetails(thatImg) 
{
    // This creates a new window and uses the hovered image name as the window 
    // name so that it can be used in the that window's javascript 
    newWindow = open("", thatImg.name,"width=400,height=500,left=410,top=210");

    // open new document 
    newWindow.document.open();


    // Text of the new document
    // Replace your " with ' or \" or your document.write statements will fail
    newWindow.document.write("<html><head><title>Movies</title>");
    newWindow.document.write("<script src='myDetails.js' type='text/javascript'>");
    newWindow.document.write("</script></head>");
    newWindow.document.write("<body bgcolor='white' onload='popUpDetails();'>");
    newWindow.document.write("... SOME OTHER HTML....");
    newWindow.document.write("</body></html>");


    // close the document
    newWindow.document.close(); 
}



// This is the function that will call the
// closeWindow() after 2 seconds
// when the mouse is moved off the image.
function closeDetails() 
{
    winCloseTimer = window.setTimeout("closeWindow();", 2000);
}

// This function closes the pop-up window
// and turns off the Window Timers
function closeWindow()
{
    // If popUpHover == true then I do not want
    // the window to close
    if(popUpHover == false)
    {
        clearInterval(winOpenTimer); 
        clearInterval(winCloseTimer);
        newWindow.close();
    }
}

function popUpDetails()
{
    // This will be used to prevent the Details Window from closing
    popUpHover = true;

    // Below is some other javascript code...
}
</script> 
4

1 に答える 1

1

このタスクに新しいブラウザ ウィンドウを使用しないことをお勧めします。次のようなことを試してください:

var popup = {
  open = function () {
    if (this.element == null) {
      // create new div element to be our popup and store it in the popup object 
      this.element = document.createElement('div');
      this.element.id = "myPopup";
      // you don't need a full html document here. Just the stuff you were putting in the <body> tag before
      this.element.innerHTML = "<your>html</here>";
      // Some bare minimum styles to make this work as a popup. Would be better in a stylesheet
      this.element.style = "position: absolute; top: 50px; right: 50px; width: 300px; height: 300px; background-color: #fff;";
    }
    // Add it to your <body> tag
    document.body.appendChild(this.element);
    // call whatever setup functions you were calling before
    popUpDetails();
  },
  close = function () {
    // get rid of the popup
    document.body.removeChild(this.element);
    // any other code you want
  }
};

// The element you want to trigger the popup
var hoverOverMe = document.getElementById("hoverOverMe");
// set our popup open and close methods to the proper events
hoverOverMe.onmouseover = popup.open;
hoverOverMe.onmouseout = popup.close;

それはそれを行う必要があります。新しいブラウザ ウィンドウよりもはるかに簡単に制御できます。CSS を自分で微調整する必要があります。

編集:

実際のウィンドウでこれを行う手順は次のとおりです。繰り返しますが、実際のウィンドウを使用することは、このタスクを達成する最善の方法ではありません。divウィンドウのように見えるようにスタイル化されたタグは、ブラウザー間で標準化された機能だけでなく、より多くの制御を提供するため、より優れています。ただし、ウィンドウを使用する必要がある場合は、次のとおりです。

// You can use many principles from the example above, but I'll give the quick version
var popup;
var hoverOverMe = document.getElementById("hoverOverMe");

hoverOverMe.onmouseover = function () {
  popup = window.open("path_to_content", "popup");
};
hoverOverMe.onmouseout = function () {
  popup.close();
};

それは機能しますが、あまりうまくいきません(私見)。ユーザーが新しいウィンドウを新しいタブで開くように設定している場合 (私のように)、タブが開きます。Javascript はそれを制御できません。Firefox では、新しいタブが開き、フォーカスが得られます。その時点でhoverOverMeonmouseout イベントが発生しているため (明らかにウィンドウが閉じます)、すぐに閉じます。実際のウィンドウでも同じ問題が発生すると思います。

于 2011-07-21T08:56:06.327 に答える