XMLHttpRequest获取特定的div [英] XMLHttpRequest to fetch a specific div

查看:51
本文介绍了XMLHttpRequest获取特定的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用XMLHttpRequest从源html文档中获取特定div

,而不是获取整个文件?


我打算在鼠标悬停时使用Ajax,但保持正常链接不变,

,这样如果用户没有javascript,他们可以点击链接

正常,这将把他们带到新的页面,但是如果他们有
javascript(并且启用了ActiveX,我想,在IE的情况下)它将只需要在当前页面中获取相应的div
。这将节省

不得不复制内容,以便Ajax可以提供服务。


当然,更改鼠标悬停内容可能会让许多用户烦恼......

有一种方法可以在页面加载时使用DOM更改链接位置,因为你可以使用
类名吗?


例如,onload函数更改< li id =" product1">< a

href =" product1.html" .... to< li id =" product1"> ;< a href ="#",以便

XMLHttpRequest可以移动到onlick事件 - 但是如果没有

javascript存在,硬链接将保持不变。


感谢您的任何指示!

Is it possible to fetch a specific div from a source html document
using XMLHttpRequest, rather than fetching the entire file?

I was going to use Ajax on mouseover but keep the normal link intact,
so that if the user does not have javascript they can click the link
as normal and this would take them to the new page, but if they have
javascript (and ActiveX enabled I guess, in the case of IE) it would
just fetch the appropriate div into the current page. This would save
having to duplicate content so that it can be served by Ajax.

Of course, changing content on mouseover could annoy many users... is
there a way to change a link location using DOM on page load, as you
might a class name?

eg an onload function changes <li id="product1"><a
href="product1.html".... to <li id="product1"><a href="#", so that the
XMLHttpRequest can be moved to an onlick event--but if there is no
javascript present, the hard link would remain intact.

thanks for any pointers!

推荐答案

br ******* @ gmail.com 在2007年2月20日上午6:03发表以下内容:
br*******@gmail.com said the following on 2/20/2007 6:03 AM:

是否可以使用XMLHttpRequest从源html文档中获取特定div

,而不是提取整个文件?
Is it possible to fetch a specific div from a source html document
using XMLHttpRequest, rather than fetching the entire file?



No.

No.


我将在鼠标悬停时使用Ajax,但保持正常链接不变,

所以,如果用户没有javascript,他们可以正常点击链接

,这会将他们带到新页面,但是如果他们有

javascript(我启用ActiveX启用,在IE的情况下),它只需要在当前页面中获取相应的div。这将节省

必须复制内容,以便它可以由Ajax提供。
I was going to use Ajax on mouseover but keep the normal link intact,
so that if the user does not have javascript they can click the link
as normal and this would take them to the new page, but if they have
javascript (and ActiveX enabled I guess, in the case of IE) it would
just fetch the appropriate div into the current page. This would save
having to duplicate content so that it can be served by Ajax.



当人们想要使用Ajax时,我喜欢它因此他们可以说我们使用Ajax。

I love it when people want to use "Ajax" so they can say "we use Ajax".


当然,更改鼠标悬停时的内容可能会让很多用户感到烦恼...是

有一种方法可以在页面加载时使用DOM更改链接位置,因为你可能是一个类名吗?
Of course, changing content on mouseover could annoy many users... is
there a way to change a link location using DOM on page load, as you
might a class name?



是的,但是为什么?

Yes, but why?


例如onload函数更改< li id =" product1" ;>< a

href =" product1.html" .... to< li id =" product1">< a href ="#",so可以将

XMLHttpRequest移动到onlick事件 - 但如果没有

javascript存在,则硬链接将保持不变。
eg an onload function changes <li id="product1"><a
href="product1.html".... to <li id="product1"><a href="#", so that the
XMLHttpRequest can be moved to an onlick event--but if there is no
javascript present, the hard link would remain intact.



这是你有一个疯狂的想法。为什么不:


< a href =" product1.html" onclick =" someAJAXFunction(); return

false"> Product< / a>


问题结束。


-

兰迪

机会有利于准备好的心灵

comp.lang.javascript常见问题 - http://jibbering.com/faq/index.html

Javascript Best实践 - http://www.JavascriptToolbox.com/bestpractices/


修改了一些现有的代码...认为它几乎就在那里,但我在哪里?
掉下来了?


var XMLHttpRequestObject = false;


if(window.XMLHttpRequest){

XMLHttpRequestObject = new XMLHttpRequest();

} else if(window.ActiveXObject){

XMLHttpRequestObject = new ActiveXObject(" Microsoft.XMLHTTP");

}


函数getDa ta(fetchdiv,targetdiv)

{

if(XMLHttpRequestObject){

var obj = document.getElementById(targetdiv);

XMLHttpRequestObject.open(" GET",fetchdiv);

XMLHttpRequestObject.onreadystatechange = function()

{

if( XMLHttpRequestObject.readyState == 4&&

XMLHttpRequestObject.status == 200){

obj.innerHTML =

XMLHttpRequestObject.responseXML。 getElementById(''d iv2'')


}

}


XMLHttpRequestObject.send(null);

}

}

modified some existing code... think it''s nearly there, but where am I
falling down?

var XMLHttpRequestObject = false;

if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}

function getData(fetchdiv, targetdiv)
{
if(XMLHttpRequestObject) {
var obj = document.getElementById(targetdiv);
XMLHttpRequestObject.open("GET", fetchdiv);
XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
obj.innerHTML =
XMLHttpRequestObject.responseXML.getElementById(''d iv2'')

}
}

XMLHttpRequestObject.send(null);
}
}


2月20日06:10,Randy Webb< HikksNotAtH ... @ aol.comwrote:
On 20 Feb, 06:10, Randy Webb <HikksNotAtH...@aol.comwrote:

brough ... @ gmail.com在2007年2月2日上午6:03发表以下内容:
brough...@gmail.com said the following on 2/20/2007 6:03 AM:

是否可以使用XMLHttpRequest从源html文档中获取特定div

,而不是获取整个f ILE?
Is it possible to fetch a specific div from a source html document
using XMLHttpRequest, rather than fetching the entire file?



No.


No.



似乎可以处理使用
获取的XML文件
XMLHttpRequest并提取特定数据,所以我认为这应该是

能够应用于html文档以便提取一个div的内容



It appears to be possible to process an XML file fetched using
XMLHttpRequest and extract specific data, so I think this should be
able to be applied to a html document in order to extract the contents
of one div.


这是你有一个疯狂的想法。为什么不:


< a href =" product1.html" onclick =" someAJAXFunction(); return

false"> Product< / a>


问题结束。
That is one crazy idea you have there. Why not:

<a href="product1.html" onclick="someAJAXFunction();return
false">Product</a>

End of problem.



啊哈!所以它没有关注链接。


如何将返回false添加到外部

脚本中的鼠标事件?例如:


var nav = document.getElementById(''test'')。getElementsByTagNa me(''a'');

for(var i = 0; i< nav.length; i ++)

{

nav [i] .onmousedown = someAJAXFunction;

}


我玩过但是someAJAXFunction;返回false似乎没什么工作。

Ah ha! So it doesn''t follow the link.

How do you add return false to mouse events that are in an external
script? eg:

var nav = document.getElementById(''test'').getElementsByTagNa me(''a'');
for (var i=0;i<nav.length;i++)
{
nav[i].onmousedown = someAJAXFunction;
}

I''ve had a play but "someAJAXFunction;return false" doesn''t seem to
work.


这篇关于XMLHttpRequest获取特定的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆