これには JavaScript を使用しないでください。最良の解決策は、プロジェクトに適切な CSS パターンを使用することです。
しかし、はい、オブジェクトを検証できます。
このようなもの:
function validateParent(element, prop, callback) {
var parentProp = element.parentElement.style[prop];
var childProp = element.style[prop];
if(parseFloat(childProp) > parseFloat(parentProp)) {
// do something here like execute the callback
if(typeof callback === 'function') {
callback.call(element, prop, parentProp);
}
}
}
使用例:
// make the children value equals the parent
validateParent(document.getElementById('foo'), 'width', function(prop, val) {
this.style[prop] = val;
});
// Dispatch an alert
validateParent(document.getElementById('foo'), 'width', function(prop, val) {
alert('The value from ' + this.id + ' is bigger then it parent, that is ' + val;
});