querySelector当元素的属性包含引号时,如何使用或任何其他 Javascript 选择器"?
例如,如果我搜索img具有srcof 的元素http://www.example.com/abc"def(是的,包含引号的属性):
document.querySelector('img[src="http://www.example.com/abc"def"]');
Run Code Online (Sandbox Code Playgroud)
它得到这个错误:
Uncaught DOMException: Failed to execute 'querySelector' on 'Document':'img[src="http://www.example.com/abc"def"]' is not a valid selector.
显然,我的问题适用于同时使用的单引号'和双"引号。
我有一个对象Foo,想要一次为其分配多个属性,类似于assign_attributesRails:
class Foo
attr_accessor :a, :b, :c
end
f = Foo.new
my_hash = {a: "foo", b: "bar", c: "baz"}
f.assign_attributes(my_hash)
Run Code Online (Sandbox Code Playgroud)
除非该类是 Rails 中的 ActiveRecord 模型,否则上述方法不起作用。有什么方法可以在 Ruby 中做到这一点吗?