我目前正在学习javascript,我发现在理解class和className之间的区别时遇到了一些困难。
我想知道class和className之间是否有任何区别。当我学习Javascript时,我发现我可以使用其中任何一个来检索特定节点的类名: 例如
/// <summary>
/// An HttpMessageHandler that lets you use the Windows Runtime IHttpFilter types
/// </summary>
public class CustomHttpClientHandler : HttpMessageHandler
{
static readonly List<ChainValidationResult> AllowedCertificateErrors =
new List<ChainValidationResult>(){
ChainValidationResult.Expired,
ChainValidationResult.Untrusted,
ChainValidationResult.InvalidName,
};
private static readonly Version NoVersion = new Version(0, 0);
private static readonly Version Version10 = new Version(1, 0);
private static readonly Version Version11 = new Version(1, 1);
private static readonly Version Version20 = new Version(2, 0);
private readonly rt.HttpClient _client;
private bool _disposed = false;
public CustomHttpClientHandler()
{
var filter = new HttpBaseProtocolFilter();
foreach (var error in AllowedCertificateErrors)
filter.IgnorableServerCertificateErrors.Add(error);
_client = new rt.HttpClient(filter);
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
CheckDisposed();
var rtMessage = await ConvertHttpRequestMessageToRt(request, cancellationToken).ConfigureAwait(false);
var resp = await _client.SendRequestAsync(rtMessage).AsTask(cancellationToken).ConfigureAwait(false);
var netResp = await ConvertRtResponseMessageToNet(request, resp, cancellationToken).ConfigureAwait(false);
return netResp;
}
internal static async Task<rt.HttpRequestMessage> ConvertHttpRequestMessageToRt(HttpRequestMessage message, CancellationToken token)
{
var rt = new rt.HttpRequestMessage()
{
Method = new rt.HttpMethod(message.Method.Method),
Content = await GetContentFromNet(message.Content).ConfigureAwait(false),
RequestUri = message.RequestUri,
};
CopyHeaders(message.Headers, rt.Headers);
foreach (var prop in message.Properties)
rt.Properties.Add(prop);
return rt;
}
internal static void CopyHeaders(IEnumerable<KeyValuePair<string, IEnumerable<string>>> source, IDictionary<string, string> destination)
{
var headers = from kvp in source
from val in kvp.Value
select new KeyValuePair<string, string>(kvp.Key, val);
destination.Clear();
foreach (var header in headers)
destination.Add(header);
}
internal static async Task<rt.IHttpContent> GetContentFromNet(HttpContent content)
{
if (content == null)
return null;
rt.IHttpContent c;
if (content is StringContent)
c = new rt.HttpStringContent(await content.ReadAsStringAsync().ConfigureAwait(false),
Windows.Storage.Streams.UnicodeEncoding.Utf8);
else
{
var stream = await content.ReadAsStreamAsync().ConfigureAwait(false);
c = new rt.HttpStreamContent(stream.AsInputStream());
}
CopyHeaders(content.Headers, c.Headers);
return c;
}
internal static async Task<HttpContent> GetNetContentFromRt(rt.IHttpContent content, CancellationToken token)
{
if (content == null)
return null;
var str = await content.ReadAsInputStreamAsync().AsTask(token).ConfigureAwait(false);
var c = new StreamContent(str.AsStreamForRead());
foreach (var header in content.Headers)
c.Headers.TryAddWithoutValidation(header.Key, header.Value);
return c;
}
internal static async Task<HttpResponseMessage> ConvertRtResponseMessageToNet(HttpRequestMessage request, rt.HttpResponseMessage message, CancellationToken token)
{
var resp = new HttpResponseMessage((HttpStatusCode)(int)message.StatusCode)
{
ReasonPhrase = message.ReasonPhrase,
RequestMessage = request,
Content = await GetNetContentFromRt(message.Content, token).ConfigureAwait(false),
Version = GetVersionFromEnum(message.Version),
};
foreach (var header in message.Headers)
resp.Headers.TryAddWithoutValidation(header.Key, header.Value);
return resp;
}
internal static Version GetVersionFromEnum(rt.HttpVersion version)
{
switch (version)
{
case rt.HttpVersion.None:
return NoVersion;
case rt.HttpVersion.Http10:
return Version10;
case rt.HttpVersion.Http11:
return Version11;
case rt.HttpVersion.Http20:
return Version20;
default:
throw new ArgumentOutOfRangeException("version");
}
}
private void CheckDisposed()
{
if (_disposed)
throw new ObjectDisposedException("WinRtHttpClientHandler");
}
protected override void Dispose(bool disposing)
{
if (disposing && !_disposed)
{
_client.Dispose();
_disposed = true;
}
base.Dispose(disposing);
}
}
可以在http://jsfiddle.net/hphchan/3ze3ug7r/找到更详细的脚本。
我想问一下,两者之间有什么微妙的区别吗?
顺便说一下,我访问过object.className or object.getAttribute("className/class")?,并了解使用getAttribute('class')比className更通用,因为className只适用于HTML,而不适用于SVG等其他类。但两者之间是否存在任何额外差异?
感谢。
答案 0 :(得分:0)
正如@ Derek-朕会功夫所述,getAttribute
用于检索类属性的值,而直接访问className
属性则允许获取&amp;设置类名。
此外,getAttribute
函数适用于任何类型的属性,可用于轻松动态检索值,例如getAttribute(attributeName)
。