在我的胡子模板中,我确实有类似的东西:
<div {{attr}}="{{attrVal}}"></div>
Run Code Online (Sandbox Code Playgroud)
使用此渲染
Mustache.render(template, {attr : 'data-test', attrVal : 'test'})
Run Code Online (Sandbox Code Playgroud)
生产
<div ="test"></div>
Run Code Online (Sandbox Code Playgroud)
我希望得到类似的东西
<div data-test="test"></div>
Run Code Online (Sandbox Code Playgroud)
是不是可以使用Mustache在标签内呈现属性名称?
UPDATE
我解决了这个问题.我<template>在我的文档中的自定义标记内定义了我的HTML胡须模板.例如:
<template id='myTemplate'>
<div {{dataAttr}}="{{dataAttrValue}}"></div>
</template>
Run Code Online (Sandbox Code Playgroud)
使用document.querySelector("#myTemplate").innerHTML浏览器获取模板时确实将转换为{{dataAttr}}to,{{dataattr}}因为属性不区分大小写.所以打电话
Mustache.render(
document.querySelector("#myTemplate").innerHTML,
{ dataAttr : 'data-attr', dataAttrValue : 'test'}
);
Run Code Online (Sandbox Code Playgroud)
结果是
<div ="test"></div>
Run Code Online (Sandbox Code Playgroud) 是否可以找到以给定的绝对x/y像素坐标显示的视图?
编辑:我找到了一个合适的解决方案:
private View findViewByCoord(float x, float y){
TextView textView = null;
int[] location = new int[2];
int width = 0;
int height = 0;
for(int reference : cardReference){
textView = (TextView) findViewById(reference);
textView.getLocationOnScreen(location);
width = textView.getWidth();
height = textView.getHeight();
if(location[0] <= x && x <= (location[0] + width) && location[1] <= y && y <= (location[1] + height)){
Log.i("Test", "Card " + textView.getText() + " is pointed");
return textView;
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
其中cardReference是一个整数到资源的数组(在我的例子中,20个TextViews以4 x 5矩阵排列):
int[] cardReference …Run Code Online (Sandbox Code Playgroud) 我确实有几个由我自己创建的嵌套聚合物元素。目前,通过使用聚合物共享样式,我可以将自定义样式注入其他元素。不幸的是,这种方法仅限于静态使用。因此,在实现时,我确实需要通过使用<link rel="import" href="my-shared-style.html">和导入共享样式模块来知道哪个Element应该使用哪种共享样式<style include="my-shared-style"></style>。
但是在我的用例中,我确实需要在运行时将共享样式注入到聚合物元素中。是否有可能实现这一目标?
更新
我尝试了以下受Günters回答启发的方法:
Polymer({
is : 'html-grid-row',
/**
* Inject style into element
* @param {string} style
*/
injectStyle : function(style) {
var customStyle = document.createElement('style', 'custom-style');
customStyle.textContent = style;
Polymer.dom(this.root).appendChild(customStyle);
}
/**
* Additional Elements JS functionality is put here...
*
*/
)}
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试injectStyle('div {font-weight: bold;}')在运行时通过调用元素实例来动态添加样式时,样式模块会注入到元素中,但不会显示,因为Polymer似乎在编辑自定义样式文本内容,如下所示:
<style is="custom-style" class="style-scope html-grid-row">
div:not([style-scope]):not(.style-scope) {font-weight: bold;}
</style>
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以防止聚合物将:not([style-scope]):not(.style-scope)前缀添加到样式规则中?
更新2:
使用引用全局共享样式include='my-shared-style'确实具有相同的效果。
包括此共享样式,该样式是使用html import静态全局导入的:
<dom-module id="my-shared-style">
<template>
<style>
div { …Run Code Online (Sandbox Code Playgroud)