错误在哪里? (不能具体,因为我不知道......:/)
目标
我试图让 jQuery 将父容器的类名作为嵌套容器中的文本输出。更具体地说,我希望每个
中的所有文本都使用不同的字体进行样式设置。这很简单,我只是分配了课程。但我还希望每个
中都有一个 来显示所使用的字体(只需将父类的类名添加为文本即可)。
示例/测试
奇怪的是,
(一点)代码
第二个链接在检查时也非常方便代码,所以我希望如果我只将 jquery-stuff 粘贴到这里就可以了(我发现我很可能再次搞乱了那部分......)
<script type="text/javascript">
$(document).ready(function() {
$("span").each(function() {
var usedfont= $(this).parent().attr("class");
$(this).text(usedfont);
});
});
</script>
我的思路:
- 当 dom 加载时,
- 迭代所有span-elements,
- 设置一个变量作为父元素的类名,并将
- span-element 的文本设置为此变量。(ergo echoing 出家长的班级名称)。
问题
为什么第一个链接后面的页面不起作用?为什么 jsfiddle 测试能够成功?
* 为什么 jsfiddle-result 以 });//]]>
开头?
非常感谢!
Goal
I was trying to get jQuery to output the class name of the parent container as text in a nested container. To be more specific, I wanted all text in each <p>
to be styled with a different font. That's easy enough, I just assigned classes. But I also wanted a <span>
in each <p>
to show the used font (simply by adding the parents' class name as text).
Examples/Tests
THIS link shows the not-working page
Weirdly enough,
THIS jsfiddle test (kinda*) works
(a bit of the) Code
The second link is also very convenient when checking the code, so I hope it's ok if I only paste the jquery-stuff in here (I find it very likely that I messed that part up... again...)
<script type="text/javascript">
$(document).ready(function() {
$("span").each(function() {
var usedfont= $(this).parent().attr("class");
$(this).text(usedfont);
});
});
</script>
My train of thoughs:
- when dom is loaded,
- iterate through all span-elements,
- setting a variable as the class name of the parents' element and
- setting the text of the span-element as this variable.(ergo echoing
out the parents' class name).
Question(s)
Why won't the page behind the first link work? And why does the jsfiddle test deliver?
* Why does the jsfiddle-result start out with });//]]>
?
Thanks a bunch!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案很简单: http://www.cgeese.de/ test/webfonts/js/jquery-1.7.min.js 是 404。
编辑:您的 jsfiddle 有问题,因为不需要使用小提琴 JavaScript 部分中的
标签。那里的所有代码都将被解释为 javascript。只需使用 jQuery 作为外部依赖项(列表在左侧)并删除包含您自己的 JS 的
script
标记即可。The answer is simple: http://www.cgeese.de/tests/webfonts/js/jquery-1.7.min.js is a 404.
Edit: Your jsfiddle is having issues because there's no need to use the
<script>
tags in the JavaScript portion of the fiddle. All the code there will be interpreted as javascript. Just use jQuery as an external dependency (with the list on the left) and strip out thescript
tags enclosing your own JS.jsfiddle 也可以。顶部的“奇怪的东西”来自您的代码(手动将
script
标签和 jQuery 插入已经使用 jQuery 的小提琴的 javascript 部分)And the jsfiddle is ok too. The "weird stuff" on the top comes from your code (manual inserting
script
tags and jQuery into the javascript section of a fiddle already using jQuery)