在 (jquery) ajax 请求中获取当前脚本 DOM 对象
我有一个包含一些 javascript 的 html 组件。
使用
- 该组件是模板引擎中的一个文件,因此它可以在整个 html 页面的初始渲染中
- ,作为通过 ajax 请求渲染的独立 html
javascript 应该应用于模板中的对象,即:
<div class="grid" >
<div class="item" id="item_13">
This is item 13
</div>
<div class="item" id="item_14">
This is item 14
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$(HOW_DO_I_GET_PREVIOUS_ELEMENT???).someEffect(params)
})
</script>
I'已经检查了这个类似的问题< /a> 但最好的答案似乎依赖于当前脚本是“scripts”变量中的最后一个脚本,因为下一个脚本尚未加载。如果我用ajax请求附加html和js,就不会出现这种情况。
100%清楚:问题是如何获取前一个对象而不引用任何特定属性:标签没有唯一的ID,没有随机ID,因为理论上它总是有机会出现两次,没有唯一的类属性,如完全相同的组件可以显示在 HTML 文档的另一部分中。
I have a html component that includes some javascript.
The component is a file in a template engine, so it can be used
- in the initial rendering of the whole html page
- as stand-alone html rendered through an ajax request
The javascript should be applied to an object in the template, i.e. :
<div class="grid" >
<div class="item" id="item_13">
This is item 13
</div>
<div class="item" id="item_14">
This is item 14
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$(HOW_DO_I_GET_PREVIOUS_ELEMENT???).someEffect(params)
})
</script>
I've checked this similar question but the best answers seem to rely on the current script being the last one in the 'scripts' variable as the next ones are not loaded yet. If I append the html and js with an ajax request, it will not be the case.
To be 100% clear : the question is about getting the previous object WITHOUT reference to any specific attribute : no unique id for the tag, no random id as there is theoretically always a chance it will show up twice, no unique class attribute,as exactly the same component could be displayed in another part of the HTML document.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
涉及两步过程的简单解决方案:
1) 找出您的脚本标签是哪个元素
2) 在代码中找到该元素的前一个同级元素
:
这是好的 Web 编程吗?不。您应该知道哪些元素应该根据您生成的内容应用效果。如果你有一个带有 id 的 div,那么该 id 是唯一的,并且你的生成器可以告诉你,如果它生成该 div,它还必须生成为其设置 jQuery 效果的 js。
但让我们忽略这一点;有效吗?就像一个魅力。
Simple solution involving a two step process:
1) find out which element your script tag is
2) find the previous sibling of that element
in code:
Is this good web programming? No. You should know which elements should have effects applied to them based on what you're generating. If you have a div with an id, that id is unique, and your generator can tell that if it generates that div, it will also have to generate the js that sets up the jQuery effect for it.
But let's ignore that; does it work? Like a charm.
如果您可以为您的
块提供一个 Id,您可以轻松调用
prev()
来获取前一个元素。jsfiddle 上的示例。
If you can give your
<script/>
block an Id you could easily callprev()
to get the previous element.Example on jsfiddle.
您需要找到一种方法来在“grid”div 之后立即引用脚本标记。正如 @Mark 所说,最简单的方法是为脚本标签提供唯一的 id。如果这超出了您的控制范围,但您确实可以控制脚本内容(由于您正在创建脚本而隐含),您可以执行以下操作:
Hack?是的。有效吗?另外,是的。
You will need to get a way to reference the script tag immediately after the "grid" div. As @Mark stated, the easiest way to do this is by giving the script tag a unique id. If this is beyond your control, but you do have control of the script contents (implicit by the fact that you are creating it) you can do something like this:
Hack? Yes. Does it Work? Also, yes.
这是在 FF、Chrome 和 IE 8 中工作的东西,在其他地方没有尝试过。它查看页面上最后一个元素之前的元素(正在解析的脚本),将其存储在本地(使用自调用函数),以便加载处理程序可以使用它。
http://jsfiddle.net/MtQ5R/2/
话虽如此。我仍然必须提到,您将行为 (JS) 和 HTML 紧密耦合在一起,将它们放入同一个文件中,这违背了分离它们的 Web 流程。另外,我不知道您希望它如何与 AJAX 请求一起使用,因为您不仅仅是在渲染时将其添加到 HTML 中。在这种情况下,很容易获得对刚刚插入的 html 的引用。
Here's something that works in FF, Chrome and IE 8, untried anywhere else. It looks at the element before the last element on the page (which is the script being parsed), stores it locally (with a self calling function) so the load handler can use it.
http://jsfiddle.net/MtQ5R/2/
Having said that. I still have to mention that you're tightly coupling the behavior (JS) and HTML, by putting them into the same file which kind of goes against the web flow of separating them. Also, I don't know how you'd expect this to work with an AJAX request since you're not just adding it to the HTML as it's being rendered. In that case, it would be very easy to get a reference to the html you just inserted though.