在 Javascript 中将 Javascript 嵌入到 HTML 中(Oi!)
好吧...我正在尝试编写一些适用于 html 块的 javascript 代码,该代码本身出现在 javascript 文件中。真是一团糟。这是一个示例:
[new Date(1942,01,01), , '<div onclick="this.nextSibling.style.display=\'block\'; this.style.display=\'none\'"><img src="img/content/michenerCenterThumb.jpg" style="cursor:pointer" /></div><div style="display:none"><iframe width="200" height="165" src="http://www.youtube.com/embed/in7IUzjN3pY?rel=0" frameborder="0" allowfullscreen></iframe>'],
[编辑]我正在使用时间轴,可在此处使用:http://links.sourceforge。网/#timeline。构建时间线的一部分涉及添加条目,如本示例所示:
data.addRows([
[new Date(2010,7,23), , 'Conversation<br>' +
'<img src="img/comments-icon.png" style="width:32px; height:32px;">'],
[new Date(2010,7,23,23,0,0), , 'Mail from boss<br>' +
'<img src="img/mail-icon.png" style="width:32px; height:32px;">'],
[new Date(2010,7,24,16,0,0), , 'Report'],
[new Date(2010,7,26), new Date(2010,8,2), 'Traject A'],
[new Date(2010,7,28), , 'Memo<br>' +
'<img src="img/notes-edit-icon.png" style="width:48px; height:48px;">'],
[new Date(2010,7,29), , 'Phone call<br>' +
'<img src="img/Hardware-Mobile-Phone-icon.png" style="width:32px; height:32px;">'],
[new Date(2010,7,31), new Date(2010,8,3), 'Traject B'],
[new Date(2010,8,4,12,0,0), , 'Report<br>' +
'<img src="img/attachment-icon.png" style="width:32px; height:32px;">']
]);
在 html 中,我想实现 jquery ui、highslider 等,但这很快就会变得混乱(如我上面的示例)。什么是“正确执行” “在这种情况下?
谢谢,
本
Alright... I'm trying to write some javascript code that will apply to an html block, which itself appears in a javascript file. It's a complete mess. Here's a sample:
[new Date(1942,01,01), , '<div onclick="this.nextSibling.style.display=\'block\'; this.style.display=\'none\'"><img src="img/content/michenerCenterThumb.jpg" style="cursor:pointer" /></div><div style="display:none"><iframe width="200" height="165" src="http://www.youtube.com/embed/in7IUzjN3pY?rel=0" frameborder="0" allowfullscreen></iframe>'],
[edit] I'm using Timeline, which is available here: http://links.sourceforge.net/#timeline. Part of building a timeline involves adding entries, like in this sample:
data.addRows([
[new Date(2010,7,23), , 'Conversation<br>' +
'<img src="img/comments-icon.png" style="width:32px; height:32px;">'],
[new Date(2010,7,23,23,0,0), , 'Mail from boss<br>' +
'<img src="img/mail-icon.png" style="width:32px; height:32px;">'],
[new Date(2010,7,24,16,0,0), , 'Report'],
[new Date(2010,7,26), new Date(2010,8,2), 'Traject A'],
[new Date(2010,7,28), , 'Memo<br>' +
'<img src="img/notes-edit-icon.png" style="width:48px; height:48px;">'],
[new Date(2010,7,29), , 'Phone call<br>' +
'<img src="img/Hardware-Mobile-Phone-icon.png" style="width:32px; height:32px;">'],
[new Date(2010,7,31), new Date(2010,8,3), 'Traject B'],
[new Date(2010,8,4,12,0,0), , 'Report<br>' +
'<img src="img/attachment-icon.png" style="width:32px; height:32px;">']
]);
In the html, I want to implement jquery ui, highslider, etc., but this becomes messy very fast (like in my above example.) What is "doing it right" in this instance?
Thanks,
Ben
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想出了一个更优雅的解决方案......我希望更接近正确编码的构成。现在每个元素的html都写在html页面上了。我通过使用自定义 getElementsByClassName 来阅读此内容,它将每个项目类插入到一个数组中。然后该数组填充时间线。
html:
和 javascript:
您可以在此处找到自定义 getElementsByClassName:http://code.google.com /p/getelementsbyclassname/
I came up with a more elegant solution... closer, I hope, to what constitutes proper coding. The html of each element is now written on the html page. I read this by using a custom getElementsByClassName which inserts each item class into an array. This array then populates the timeline.
The html:
and the javascript:
You can find the custom getElementsByClassName here: http://code.google.com/p/getelementsbyclassname/