简体   繁体   English

使用#和是否安全。 html属性中的字符?

[英]Is it safe to use # and . characters in html attributes?

I'm wondering if I can set the value of an HTML object's attribute as a string that contains # character ? 我想知道我是否可以将HTML对象的属性值设置为包含#字符串?

The reason I want to do this is the page will have a lot of items that should scroll the page to specified elements, and I want to store the data ' which item should it scroll to? 我想这样做的原因是页面将有很多项目应该将页面滚动到指定的元素,我想存储数据'滚动到哪个项目? ' as a data-scrollto attribute. '作为data-scrollto属性。

So my JavaScript code -will- look like this: 所以我的JavaScript代码 - 将会是这样的:

function ScrollToElement(lm)
{
    var theTop  = lm.offset().top;
    $("html,body").animate({
        scrollTop: theTop/*,
        scrollLeft: 1000*/
    });
}

// .. and add click event to all such objects : 
$('.scroller').click(function(){
        var theSelector = $(this).attr('data-scrollto');
        ScrollToElement($(theSelector));
    });

so the html elements will look like : 所以html元素看起来像:

<a class='scroller' data-scrollto='p#p-to-scroll'>Click to scroll to p</a>

is it safe ? 这样安全吗?

And as a side question, why does 作为一个附带问题,为什么呢

$(element).data('scrollto');

does not work but 不起作用但是

$(element).attr('data-scrollto'); 

works ? 工作?

According to the W3C specs, yes, it is safe to use the U+0023 NUMBER SIGN characters ( # ) and the U+002E FULL STOP characters ( . ). 根据W3C规范,是的,它安全的,使用U+0023 NUMBER SIGN字符( # )和U+002E FULL STOP字符( . )。

The attribute name, followed by zero or more space characters, followed by a single U+003D EQUALS SIGN character, followed by zero or more space characters, followed by the attribute value, which, in addition to the requirements given above for attribute values, must not contain any literal space characters, any U+0022 QUOTATION MARK characters ( " ), U+0027 APOSTROPHE characters ( ' ), U+003D EQUALS SIGN characters ( = ), U+003C LESS-THAN SIGN characters ( < ), U+003E GREATER-THAN SIGN characters ( > ), or U+0060 GRAVE ACCENT characters (`), and must not be the empty string . 属性名称,后跟零个或多个空格字符,后跟单个U+003D EQUALS SIGN字符,后跟零个或多个空格字符,后跟属性值,除了上面给出的属性值要求之外, 不得包含任何文字空格字符,任何U+0022 QUOTATION MARK字符( " ), U+0027 APOSTROPHE字符( ' ), U+003D EQUALS SIGN字符( = ), U+003C LESS-THAN SIGN字符( < ), U+003E GREATER-THAN SIGN字符( > )或U+0060 GRAVE ACCENT字符(`),不能是空字符串

Read http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#attributes-0 . 阅读http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#attributes-0

Yes, you can store a hash # and . 是的,你可以存储哈希#. in the data attributes. 在数据属性中。 You should use the proper data methods. 您应该使用正确的数据方法。 http://api.jquery.com/jQuery.data/ http://api.jquery.com/jQuery.data/

var theSelector = $(this).data('scrollto');

If you can't seem to use the data method, check the version of jQuery you are using, it was introduced in version 1.2.3 如果您似乎无法使用数据方法,请检查您正在使用的jQuery版本,它是在1.2.3版本中引入的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM