0

我试图发现的是当内部函数和事件监听器被执行时如何设置一个值。如果可以做到这一点,那么该值可以用于控制其他潜在操作。由于没有找到关于此的精确材料,我一直在尝试。下面的代码不会使用 Chrome JS 控制台发出任何警报,它会在鼠标离开 MoscowNights Div 时执行 aler(77),但整个 claptrap 代码块的后端没有任何价值。

function IsolateThaBUG() {
  var MoscowNights = document.getElementById("MoscowNights");

  MoscowNights.addEventListener("mouseout", function(evt) {
    var MaloMaloJavaScript = 77;
    alert(MaloMaloJavaScript);
    return MaloMaloJavaScript;
  }, false);

  var Convoluted = MoscowNights;
  return Convoluted;
}

var GotAnythingYET = IsolateThaBUG();
alert('Got This NONSENSE Instead : ' + GotAnythingYET);

对,嗯……下一个块也没有放弃一个价值……或者看起来是这样。如果它是可访问的,它应该出现在 div 中,但它读取为undefined所以......也许没有办法一次性做到这一点。

    function IsolateThaBUG(){
       var MoscowNights = document.getElementById('MoscowNights');
        var MaloMaloJavaScript; // temp undefnd
        MoscowNights.addEventListener("mouseout", function (evt) {
            var MaloMaloJavaScript=77;   <<<<<<<<<<<<<<<<<<<<<<<    THAT,
            }, false);
        return MaloMaloJavaScript;   >>>>>>>>>>>>    is just NOT getting OUTSIDE!!
    }
     var GotAnythingYET = IsolateThaBUG();
     function WritetestValue() {
        var artfulArtificial = document.getElementById("whattheSAMHILLsigoinon");
        var TitleWrite = artfulArtificial.appendChild(document.createTextNode(testValue));
        return TitleWrite;
    }
    WritetestValue();

我接受的答案,正如我在研究它时发现的那样,安装并测试了它。

4

1 回答 1

0

您编写的函数IsolateThaBug正在返回 的值MoscowNights,它是一个元素。

我推测您希望检索MaloMaloJavascript. 为了实现这一点,您需要传入一个回调或其他一些变量来进行变异。

这里有一些例子。

function testMouseout() {
  var element = document.getElementById('MoscowNights'); 
  var testValue;  // undefined currently.
  element.addEventListener('mouseout', function(e) {
    testValue = 77;
  }, false);
  return testValue;
}

var mouseoutTest = testMouseout(); 
console.log(mouseoutTest); // undefined
// perform the mouseout action. If you did, the value will be 77.
window.setTimeout(function(){
  console.log(mouseoutTest) // 77
}, 5000)

如果您想等到鼠标移出发生后再触发警报,您​​可以传入一个回调函数。这利用了 JavaScript 的一流函数。

function testMouseout(callback) {
  var element = document.getElementById('MoscowNights'); 
  var testValue;  // undefined currently.
  element.addEventListener('mouseout', function(e) {
    testValue = 77;
    callback(testValue);
  }, false);
  return testValue;
}

// we pass in a function that will 
// be executed when the event fires.
testMouseout(function(value) {
  alert(value); // 77
});
于 2013-02-10T02:27:23.650 回答