这很奇怪。我试图添加事件监听器,但它没有工作,虽然它硬编码到html工作。
这是我的代码 -
<script src="js/dynamo.js"></script>
<link rel="stylesheet" type="text/css" href="css/reset.css">
<script>
window.onload = function() {
Dynamo.intilize();
Dynamo.loadAndReplace("js/index.json");
};
</script>
</head>
<body>
<main>
<a href="test">tet</a>
Dynamo.intilize function =
var Dynamo = {
__elements: document.getElementsByTagName("*"),
intilize: function() {
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
var link = links[i].attributes[0].value;
if (link.match(/^https?/) === null) {
console.log(links[i]);
links[i].addEventListener("mouseover",
function(event) {
event.preventDefault();
alert('event');
//Dynamo.loadAndReplace('js/' + link + '.json');
// history.pushState(null, null, links[i].href);
}
, false);
}
}
},
manipulateValue: function(elem, manipulation) {
for (var i = 0; i < elem.attributes.length; i++) {
elem.setAttribute(elem.attributes[i].name, manipulation(elem.attributes[i].value));
}
},
replace: function(str, match, replacement) {
return str.replace(new RegExp('\{\{' + match + '\}\}', "g"), replacement);
},
loadAndReplace: function(json) {
$.getJSON(json, function(data) {
var keys = Object.keys(data);
for (var i = 0; i < Dynamo.__elements.length; i++) {
if (Dynamo.__elements[i].attributes.length !== 0) {
Dynamo.manipulateValue(Dynamo.__elements[i],
function(val) {
for (var j = 0; j < keys.length; j++) {
return val.replace('{{' + keys[j] + '}}', data[keys[j]]);
}
});
} else {
for (var j = 0; j < keys.length; j++) {
Dynamo.__elements[i].innerHTML = Dynamo.replace(Dynamo.__elements[i].innerHTML, keys[j], data[keys[j]]);
}
}
}
});
}
}
但它不起作用,我不知道为什么,没有错误,什么也没有。 if部分 IS 正在工作。