我通过为他做一些网络工作来帮助他。他需要的部分内容是一种简单的方法来更改他网站上的几段文本。我没有让他编辑 HTML,而是决定提供一个包含消息的 XML 文件,我使用 jQuery 将它们从文件中提取出来并将它们插入到页面中。
它工作得很好...在 Firefox 和 Chrome 中,在 IE7 中不是很好。我希望你们中的一个能告诉我为什么。我做了一个公平的谷歌搜索,但找不到我要找的东西。
这是 XML:
<?xml version="1.0" encoding="utf-8" ?>
<messages>
<message type="HeaderMessage">
This message is put up in the header area.
</message>
<message type="FooterMessage">
This message is put in the lower left cell.
</message>
</messages>
这是我的 jQuery 调用:
<script type="text/javascript">
$(document).ready(function() {
$.get('messages.xml', function(d) {
//I have confirmed that it gets to here in IE
//and it has the xml loaded.
//alert(d); gives me a message box with the xml text in it
//alert($(d).find('message')); gives me "[object Object]"
//alert($(d).find('message')[0]); gives me "undefined"
//alert($(d).find('message').Length); gives me "undefined"
$(d).find('message').each(function() {
//But it never gets to here in IE
var $msg = $(this);
var type = $msg.attr("type");
var message = $msg.text();
switch (type) {
case "HeaderMessage":
$("#HeaderMessageDiv").html(message);
break;
case "FooterMessage":
$("#footermessagecell").html(message);
break;
default:
}
});
});
});
</script>
我需要在 IE 中做些不同的事情吗?基于带有 [object Object] 的消息框,我假设 .find 在 IE 中工作,但由于我无法使用 [0] 索引到数组或检查它的长度我猜这意味着 .find 不是返回任何结果。为什么这在 Firefox 和 Chrome 中完美运行但在 IE 中失败?
我是 jQuery 的新手,所以我希望我没有做傻事。上面的代码是从论坛中删除的,并根据我的需要进行了修改。由于 jQuery 是跨平台的,我想我不必处理这个烂摊子。
编辑:我发现如果我在 Visual Studio 2008 中加载该页面并运行它,那么它将在 IE 中运行。所以事实证明它在通过开发 Web 服务器运行时始终有效。现在我认为 IE 只是不喜欢在从我的本地驱动器加载的 XML 中执行 .find,所以也许当它在实际的 Web 服务器上时它会工作正常。
我已经确认它在从网络服务器浏览时工作正常。一定是IE的一个特点。我猜这是因为网络服务器为 xml 数据文件传输设置了 mime 类型,如果没有那个 IE 就不能正确解析 xml。
最佳答案
由于 IE 的问题是它的 xml 解析器阻塞了未使用正确的“text/xml” header 传递的 xml 文件,您可以在 Ajax complete 事件中包含一些代码:
complete: function( xhr, status )
{
alert( "COMPLETE. You got:\n\n" + xhr.responseText ) ;
if( status == 'parsererror' )
{
alert( "There was a PARSERERROR. Luckily, we know how to fix that.\n\n" +
"The complete server response text was " + xhr.responseText ) ;
xmlDoc = null;
// Create the xml document from the responseText string.
// This uses the w3schools method.
// see also
if( window.DOMParser )
{
parser=new DOMParser();
xmlDoc=parser.parseFromString( xhr.responseText,"text/xml" ) ;
}
else // Internet Explorer
{
xmlDoc=new ActiveXObject( "Microsoft.XMLDOM" ) ;
xmlDoc.async = "false" ;
xmlDoc.loadXML( xhr.responseText ) ;
}
$( '#response' ).append( '<p>complete event/xmlDoc: ' + xmlDoc + '</p>' ) ;
$( '#response' ).append( '<p>complete event/status: ' + status + '</p>' ) ;
processXMLDoc( xmlDoc ) ;
}
},
<!DOCTYPE html>
<html>
<head>
<title>Reading XML with jQuery</title>
<style>
#response
{
border: solid 1px black;
padding: 5px;
}
</style>
<script src="jquery-1.3.2.min.js"></script>
<script>
function processXMLDoc( xmlDoc )
{
var heading = $(xmlDoc).find('heading').text() ;
$( '#response' ).append( '<h1>' + heading + '</h1>' ) ;
var bodyText = $(xmlDoc).find('body').text() ;
$( '#response' ).append( '<p>' + bodyText + '</p>' ) ;
}
$(document).ready(function()
{
jQuery.ajax({
type: "GET",
url: "a.xml", // ! watch out for same
// origin type problems
dataType: "xml", // 'xml' passes it through the browser's xml parser
success: function( xmlDoc, status )
{
// The SUCCESS EVENT means that the xml document
// came down from the server AND got parsed successfully
// using the browser's own xml parsing caps.
processXMLDoc( xmlDoc );
// IE gets very upset when
// the mime-type of the document that
// gets passed down isn't text/xml.
// If you are missing the text/xml header
// apparently the xml parse fails,
// and in IE you don't get to execute this function AT ALL.
},
complete: function( xhr, status )
{
alert( "COMPLETE. You got:\n\n" + xhr.responseText ) ;
if( status == 'parsererror' )
{
alert( "There was a PARSERERROR. Luckily, we know how to fix that.\n\n" +
"The complete server response text was " + xhr.responseText ) ;
xmlDoc = null;
// Create the xml document from the responseText string.
// This uses the w3schools method.
// see also
if( window.DOMParser )
{
parser=new DOMParser();
xmlDoc=parser.parseFromString( xhr.responseText,"text/xml" ) ;
}
else // Internet Explorer
{
xmlDoc=new ActiveXObject( "Microsoft.XMLDOM" ) ;
xmlDoc.async = "false" ;
xmlDoc.loadXML( xhr.responseText ) ;
}
$( '#response' ).append( '<p>complete event/xmlDoc: ' + xmlDoc + '</p>' ) ;
$( '#response' ).append( '<p>complete event/status: ' + status + '</p>' ) ;
processXMLDoc( xmlDoc ) ;
}
},
error: function( xhr, status, error )
{
alert( 'ERROR: ' + status ) ;
alert( xhr.responseText ) ;
}
});
});
</script>
</head>
<body>
<div>
<h1><a href="http://think2loud.com/reading-xml-with-jquery/">Reading XML with jQuery</a></h1>
<p>
<a href="http://docs.jquery.com/Ajax/jQuery.ajax#options">#1 jQuery.ajax ref</a>
</p>
</div>
<p>Server says:</p>
<pre id="response">
</pre>
</body>
</html>
<?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
它扩展了this example .
关于javascript - jQuery .find() 在 IE 中不返回数据,但在 Firefox 和 Chrome 中返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/562283/
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
所以我开始关注ruby,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出
rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送
为什么以下不同?Time.now.end_of_day==Time.now.end_of_day-0.days#falseTime.now.end_of_day.to_s==Time.now.end_of_day-0.days.to_s#true 最佳答案 因为纳秒数不同:ruby-1.9.2-p180:014>(Time.now.end_of_day-0.days).nsec=>999999000ruby-1.9.2-p180:015>Time.now.end_of_day.nsec=>999999998
在Ruby1.9.3(可能还有更早的版本,不确定)中,我试图弄清楚为什么Ruby的String#split方法会给我某些结果。我得到的结果似乎与我的预期相反。这是一个例子:"abcabc".split("b")#=>["a","ca","c"]"abcabc".split("a")#=>["","bc","bc"]"abcabc".split("c")#=>["ab","ab"]在这里,第一个示例返回的正是我所期望的。但在第二个示例中,我很困惑为什么#split返回零长度字符串作为返回数组的第一个值。这是什么原因呢?这是我所期望的:"abcabc".split("a")#=>["bc"