3

私の webApp メソッドの 1 つで、html 要素を動的に作成し、このコードを使用して動的に作成された css オブジェクトを追加します。

    tag = typeof(tag) !== 'undefined' ? tag : "div";
    end = typeof(end) !== 'undefined' ? end : true;
    var html = '<'+tag+' value="'+this.id+'" class="element '+this.type+'"'+
                'style="position:absolute;z-index= '+this.id+'"'
    end ? html += "></"+tag+">" : html += "/>";
    var css = {},element = this;
    $.each(this.properties(),function(){
        var hash=this.indexOf("Color") !== -1?'#':'';
        var property = typeof(element[this])==='number'?element[this]*Nx.ratio:element[this];
        css[this]=hash+property;
    })
    console.log(css);
    html = $(html).css(css);
    $('.page[value='+page+']').append(html);

これは、作成されて css() 関数に渡された console.log から取得した css オブジェクトの例です。

Object
backgroundColor: "#ff0000"
borderColor: "#ffffff"
borderStyle: "solid"
borderWidth: "0"
height: "56.865"
left: "0"
top: "274.29"
width: "893.115"
__proto__: Object

問題は、出力要素に上、左、高さ、幅のプロパティがないことです。たとえば、次のようになります。

<div value="12" class="element rectangle" style="position: absolute; background-color: rgb(255, 0, 0); left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-top-color: rgb(255, 255, 255); border-right-color: rgb(255, 255, 255); border-bottom-color: rgb(255, 255, 255); border-left-color: rgb(255, 255, 255); border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; "></div>
4

2 に答える 2

2

あなたのコードはどういうわけか問題があるように見えます...エラーがどこにあるのか実際にはわかりませんが、;ここで欠落しています:

...'style="position:absolute;z-index= '+this.id+'"'; <-- there!

またこれ:

tag = typeof(tag) !== 'undefined' ? tag : "div"

ほとんどの場合、次のように書くこともできます。

 tag = tag || 'div'

多分あなたの問題はここにあります、たくさんthis起こっています...

$.each(this.properties(), function () {
    var hash = this.indexOf("Color") !== -1 ? '#' : '';
    var property = typeof(element[this]) === 'number' ? element[this] * Nx.ratio : element[this];
    css[this] = hash + property;
})

何ですかthis.properties()。中とthis同じですか?そこにエラーの原因のようです...thiseach(...)

于 2012-04-25T07:53:21.957 に答える
2

これらの CSS プロパティは、適切に指定していないため、要素に適用されません。lefttopwidthおよびの値は、次のheightようにする必要があります。

  • 0やなどの274.29数値
  • "0px"または、またはなどの単位接尾辞が続く文字列"274.29px"

コードは単位サフィックスのない文字列を使用しているため、値は無効と見なされ、プロパティは無視されます。

于 2012-04-25T09:11:41.330 に答える