IE, не позволяющий onClick событие на динамично созданном DOM элемент

Это полностью основано на идее @chrylis, просто процессорам не нужно менять свой API для этого, вы можете просто сделать это с аннотацией. Также есть случай, когда у вас есть несколько процессоров для одного типа.

interface StageProcessor {
    OutputType process(Stage stage);
}

@Component
@Processes(StageType.A)
class StageProcessorA implements StageProcessor{
      OutputType process(Stage stage){
        //validate stage is StageType.A
      }
}

@interface Processes{
    StageType type;
    StageType getType(){
        return type;
    }
}

@Component
@Processes(StageType.B)
class StageProcessorB implements StageProcessor{

}

@Service
class StageProcessors {
    Map<StageType, List<StageProcessor>> stageProcessors;

    StageProcessors(Collection<StageProcessor> processors) {
        Map<StageType, List<StageProcessor>> map = new HashMap<>();
        for (StageProcessor processor : processors) {
            StageType stageType = processor.getClass().getAnnotation(Processes.class).getType();
            map.computeIfAbsent(stageType, k -> new ArrayList<>()).add(processor);
        }
        stageProcessors = map;
        assert stageProcessors.size() == expectedNumberOfProcessors;
    }

    List<StageProcessor> getProcessors(StageType stage) {
        return stageProcessors.get(stage);
    }
}
7
задан Ali 21 April 2009 в 12:06
поделиться

4 ответа

Event handlers are not DOM attributes, the attribute exists in markup only - I'm not sure why FF oks this. I'll go research that now cause I want to know.

Update: seems to be mixed feelings about whether eventhandlers are DOM-valid attributes or not. Looks to me like this is MS's fault as they internally do not treat them as attributes, whilst the HTML spec indicates that they very much are. The direct consequences of this are that a number of things !IE would consider attributes cannot be set with setAttribute in IE including eventhandler bindings and importantly also style, class and name. apparently IE8 fixes this but I still haven't installed that so I can't check.

Meanwhile, for event binding use the addEventListener/attachEvent pair instead, or (less preferably because it's a direct assignment) set a.onclick directly to your target method (or more likely a closure on your method).

To fix your styling not being correctly applied use element.style = foo; or (better) element.className = bar.

Essentially the problem is setAttribute. Avoid using it.

For reference...

8
ответ дан 6 December 2019 в 21:19
поделиться

Как говорилось в annakata, вы должны использовать addEventListener / attachEvent. Однако, если вы не хотите устанавливать прямое нажатие, вы можете использовать:

  a.onclick = function() { removeThisElement(idOfParentContainer, this); };
3
ответ дан 6 December 2019 в 21:19
поделиться

На самом деле, для кросс-браузерных целей лучше использовать Jquery.

$('#exampleCA').createAppend("a", { href: '#', style: "background-color: red;", onclick: 'alert (1);' }, "aaabbb");

Это создаст тег с текстом aaabbb, href = "#", красным фоном и событием оповещения для клика.

0
ответ дан 6 December 2019 в 21:19
поделиться

Если вы ищете многоразовое решение для X-браузера, для IE, FF и Opera работает следующая функция:

  function addEvent( obj, type, fn ) {
    if ( obj.attachEvent ) {
      obj['e'+type+fn] = fn;
      obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
      obj.attachEvent( 'on'+type, obj[type+fn] );
    } else
      obj.addEventListener( type, fn, false );
  }
0
ответ дан 6 December 2019 в 21:19
поделиться
Другие вопросы по тегам:

Похожие вопросы: